Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
July-G opened this issue Sep 9, 2016 · 6 comments
Closed

Comments

@July-G
Copy link

July-G commented Sep 9, 2016

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
Copy link
Contributor

@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
Copy link
Author

July-G commented Sep 9, 2016

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
Copy link
Contributor

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
Copy link
Author

July-G commented Sep 9, 2016

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
Copy link
Contributor

@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
Copy link
Owner

stefanbirkner commented Sep 16, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants