Closed ghost closed 8 years ago
Does the problem still exists if you run the test without the PowerMockRunner
?
The problem seems to be the PowerMockRunner. The test is successful if I don't run it with the PowerMockRunner.
This is a PowerMock issue: jayway/powermock#427. I created a pull request for PowerMock that solves the problem. I found a workaround, too. The issue does not apply if there is only one rule for a test. This can be achieved by using org.junit.rules.RuleChain
:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Program.class)
public class ProgramTest
{
private final ExpectedSystemExit exit = ExpectedSystemExit.none();
private final SystemErrRule err = new SystemErrRule().enableLog().muteForSuccessfulTests();
private final SystemOutRule out = new SystemOutRule().enableLog().muteForSuccessfulTests();
@Rule
public final TestRule rule = RuleChain.outerRule(exit).around(err).around(out);
...
Thank you for the deep look into this, as well as the provided workaround.
I'll have a chance to test that it works as you've suggested this weekend and will watch for implementation on the PowerMock side.
Since this isn't an issue with the System Rules implementation, this seems like it can be closed.
While creating unit tests to handle some command line output and exceptions thrown in an application, I decided to use SystemErrRule and SystemOutRule to assist with this.
As this particular test is for the main entry point of the application, it is parsing the command line and calling System.exit(1) when invalid command line arguments have been provided.
However, including both of these causes Gradle to throw an exception when trying to run the task
gradlew test
:In IntelliJ, trying to run this test results in the error response "Failed to start: 0 passed, 1 not started" - so it isn't isolated to Gradle.
Here is an example setup that can reproduce this issue:
Program.java:
ProgramTest.java:
Environment Configuration: