• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

junit asserts for log messages

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I assert that my code under test has executed a code path with logger output? I'm guessing that you could do this via a listener, but I'm not having any luck finding the "recipe" to do this.

I'm using junit and java.util.logging.Logger.

code under test:
if (...) {
...
} else {
LOG.severe("condition was false");
}

test pseudocode:

// assertLOG count = 1
// assertLOG level = severe
// assertLOG message contains("condition was false")
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the code under test obtain the LOG instance? It would be convenient from a testing perspective if you could substitute a mock object or another type of test double for the logger. That way you'd get access to everything the code under test writes into the log.

Another option (uglier but an option nevertheless) might be to configure your logging framework with a custom logger implementation when you're running tests. That custom logger might write everything into an in-memory buffer rather than a physical file and expose the contents of that buffer to anyone interested.

Yet another option (still uglier but an option nevertheless) might be to read the actual, physical log file from your test.

Does this help?
 
Jeff Black
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply. I think we'll have to work with option 2. The loggers are all created directly:

private static final Logger LOG = Logger.getLogger("com.company.BuildTimeTrend");

so if we can configure the logging framework via code to capture the messages in memory, that would work out fairly well.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Black:
so if we can configure the logging framework via code to capture the messages in memory, that would work out fairly well.


You might be able to get by just by having a different configuration file for test vs. production. For example, I've had separate source trees for production code and test code and separate "resource" directories as well, meaning that I could do things like:

Now, when you configure your build/IDE to put the stuff in "src/test/*" before "src/main/*" to your classpath, the test configuration will be used when you run tests. When you package up the application for deployment, however, you exclude "src/test/*" altogether and end up with the production configuration.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I found this old thread while searching for a solution to the same problem.

In the end I came up with something simple that seems to be working (so far)...


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using an Apache log4j Logger you can use this test pattern ...

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic