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

Fix ExternalResource: the test failure was lost.. #1335

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/java/org/junit/rules/ExternalResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.junit.rules;

import java.util.ArrayList;
import java.util.List;

import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;

/**
Expand Down Expand Up @@ -44,11 +48,20 @@ private Statement statement(final Statement base) {
@Override
public void evaluate() throws Throwable {
before();

List<Throwable> errors = new ArrayList<Throwable>();
try {
base.evaluate();
} catch(Throwable t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style-nit : please put a space after "catch" (we use Google Java style which requires spaces around keywords like "if" and "catch"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to push?

errors.add(t);
} finally {
after();
try {
after();
} catch(Throwable t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue here, too

errors.add(t);
}
}
MultipleFailureException.assertEmpty(errors);
}
};
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/junit/rules/ExternalResourceRuleTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.junit.rules;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.runners.statements.Fail;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;

public class ExternalResourceRuleTest {
private static String callSequence;
Expand Down Expand Up @@ -41,4 +48,28 @@ public void externalResourceGeneratesCorrectSequence() {
assertThat(testResult(UsesExternalResource.class), isSuccessful());
assertEquals("before test after ", callSequence);
}

@Test
public void shouldThrowMultipleFailureExceptionWhenTestFailsAndClosingResourceFails() throws Throwable {
// given
ExternalResource resourceRule = new ExternalResource() {
@Override
protected void after() {
throw new RuntimeException("simulating resource tear down failure");
}
};
Statement failingTest = new Fail(new RuntimeException("simulated test failure"));
Description dummyDescription = Description.createTestDescription(
"dummy test class name", "dummy test name");

try {
resourceRule.apply(failingTest, dummyDescription).evaluate();
fail("ExternalResource should throw");
} catch (MultipleFailureException e) {
assertThat(e.getMessage(), allOf(
containsString("simulated test failure"),
containsString("simulating resource tear down failure")
));
}
}
}