I notice some weird behavior using standard logging during JUnit tests. Does JUnit translate standard output to another thread? How can I access this?
Here is a simple JUnit test that demonstrates the behavior that I am describing.
@Test public void logMessage() { // set up new logger with output directed to standard out Logger logger = Logger.getLogger("my.test.logger"); logger.addHandler(new StreamHandler(System.out, new SimpleFormatter())); // log a warning message logger.warning("logger message"); // message 1 // turn off parent handlers logger.setUseParentHandlers(false); // log a second warning message logger.warning("second logger message"); // message 2 // print somehting to standard output System.out.println("standard output message"); //message 3 }
Note that I created a new log that simply sends its log messages to standard output (System.out).
Here's the Junit output
Testsuite: com.my.FormatterTest Feb 19, 2009 12:02:33 PM com.my.FormatterTest logMessage WARNING: logger message standard output message Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.079 sec ------------- Standard Output --------------- standard output message ------------- ---------------- --------------- ------------- Standard Error ----------------- Feb 19, 2009 12:02:33 PM com.my.FormatterTest logMessage WARNING: logger message ------------- ---------------- --------------- Feb 19, 2009 12:02:33 PM com.my.FormatterTest logMessage WARNING: logger message Feb 19, 2009 12:02:33 PM com.my.FormatterTest logMessage WARNING: second logger message test: BUILD SUCCESSFUL (total time: 2 seconds)
Why does message 1 or message 2 not appear in the Standard Output section of JUnit output?
Thanks!
source share