Two Laptop Bag*
The moose likes Testing and the fly likes junit asserts for log messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "junit asserts for log messages" Watch "junit asserts for log messages" New topic
Author

junit asserts for log messages

Jeff Black
Greenhorn

Joined: Aug 02, 2002
Posts: 14
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")
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jeff Black
Greenhorn

Joined: Aug 02, 2002
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
Michael Bedward
Greenhorn

Joined: Dec 10, 2009
Posts: 3
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)...


Aaron Chesny
Greenhorn

Joined: Nov 02, 2011
Posts: 1
If you are using an Apache log4j Logger you can use this test pattern ...

 
Consider Paul's rocket mass heater.
 
subject: junit asserts for log messages