stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

SystemOutRule.getLog() does not work when I running all tests in a Test file #45

Closed July-G closed 8 years ago

July-G commented 8 years ago

Using system-rules 1.16 with Junit, Intellij IDEA and Maven It works just fine when I run my test one by one from my IDEA right click menu, say "Run 'testCanNotRestart()'".

But does not work when I run all test methods in the test file, in my case there are other 4 test methods there. Also, it will fail when i run "maven test”.

In the above two fail cases, the SystemOutRule.getLog() just return nothing.

jukecraft commented 8 years ago

@July-G I had a similar problem when using a logging library and some of the tests were changing the appenders, removing the one that logs to the console and replacing it with an asynchronous one that had 80% success rate...

could you provide us with a sample project (minimum code to reproduce the problem)?

July-G commented 8 years ago

Actually it's quite a simple test, the two test methods are like below:

public void test(){
systemOutRule.clearLog();
//doing my business log that generate log
String log=systemOutRule.getLog();
//doing assert
}
jukecraft commented 8 years ago

Dear July,

I'm afraid that code alone is not enough to reproduce the problem on it's own.

Maybe the clue is in your pom file?

What kind of logging library are you using?

July-G commented 8 years ago

LOGGERS

log4j.rootLogger=info, other, console

APPENDERS

log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%t][%-5p]%d{yyyy-MM-dd HH:mm:ss.SSS} [%c{1}.%M] - %m%n log4j.appender.console.encoding= UTF-8

消息

log4j.appender.other=org.apache.log4j.RollingFileAppender log4j.appender.other.File=${home}/log/other.log log4j.appender.other.Append=false log4j.appender.other.encoding = UTF-8 log4j.appender.other.MaxFileSize=200MB log4j.appender.other.MaxBackupIndex=10 log4j.appender.other.layout=org.apache.log4j.PatternLayout log4j.appender.other.layout.ConversionPattern=[%t][%-5p]%d{yyyy-MM-dd HH:mm:ss.SSS} [%c{1}.%M] - %m%n

jukecraft commented 8 years ago

@July-G can you post the list of dependencies from the log file and the complete test class? We need to know how you configure logging in the test and in other tests that run at the same time. Can you narrow down which test running together with this one influences the outcome? If the logger is being configured for both tests, one might override the other and therefore the code executed in your test no longer logs to the console. You can debug and trace the log call to see which appenders are listening to the logging call in both cases. System Rules can only detect what is written to the console.

stefanbirkner commented 8 years ago

This problem is very similar to #40. It looks like you're using Log4J 1.x. The problem is that org.apache.log4j.ConsoleAppender stores a reference to System.out internally. System Rules cannot modify this reference to the real System.out.

You can test that your Java code creates the correct logging events with a rule like in junit-team/junit4#1230.