Skip to content

Commit

Permalink
Merge pull request junit-team#521 from mmichaelis/adjust-testwatcher-…
Browse files Browse the repository at this point in the history
…skipped

Adjust testwatcher skipped to handle failures correctly
  • Loading branch information
David Saff committed Nov 6, 2012
2 parents 35505c4 + be61237 commit 94aa692
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/org/junit/rules/TestWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void evaluate() throws Throwable {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
skipped(e, description);
throw e;
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
Expand Down Expand Up @@ -87,6 +87,15 @@ private void failedQuietly(Throwable t, Description description,
}
}

private void skippedQuietly(AssumptionViolatedException e, Description description,
List<Throwable> errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}

private void startingQuietly(Description description,
List<Throwable> errors) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
Expand All @@ -37,6 +38,29 @@ public void neitherLogSuccessNorFailedForViolatedAssumption() {
is("starting finished "));
}

public static class TestWatcherSkippedThrowsExceptionTest {
@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
throw new RuntimeException("watcher failure");
}
};

@Test
public void fails() {
throw new AssumptionViolatedException("test failure");
}
}

@Test
public void testWatcherSkippedThrowsException() {
PrintableResult result = testResult(TestWatcherSkippedThrowsExceptionTest.class);
assertThat(result, failureCountIs(2));
assertThat(result, hasFailureContaining("test failure"));
assertThat(result, hasFailureContaining("watcher failure"));
}

public static class FailingTest {
private static StringBuilder watchedLog = new StringBuilder();

Expand Down

0 comments on commit 94aa692

Please sign in to comment.