This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
Joined: Jan 23, 2002
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.