Closed July-G closed 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)?
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
}
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?
log4j.rootLogger=info, other, console
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
@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.
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.
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.