I'm noticing some weird behavior using standard logging during JUnit tests. Does JUnit redirect standard output to a different stream? How can I get access to that?
Here's a simple JUnit test that demonstrates the behavior I'm 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
}
Notice that I've created a new logger 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 don't message 1 or message 2 show up in the Standard Output portion of the JUnit output?
Thanks!