Skip to content

Commit

Permalink
Handle expected AssertionError and AssumptionViolatedException.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbirkner committed Aug 6, 2013
1 parent 1028d1c commit 5085af7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.junit.Assert.fail;
import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;

import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.internal.AssumptionViolatedException;
Expand Down Expand Up @@ -199,11 +198,16 @@ private void optionallyHandleException(Throwable e, boolean handleException)
throws Throwable {
if (handleException) {
handleException(e);
} else {
} else if (!isExpectedException(e)) {
throw e;
}
}

private boolean isExpectedException(Throwable e) {
return fMatcherBuilder.expectsThrowable()
&& fMatcherBuilder.build().matches(e);
}

private void handleException(Throwable e) throws Throwable {
if (fMatcherBuilder.expectsThrowable()) {
assertThat(e, fMatcherBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ public static class ThrowExpectedAssertionError {

@Test
public void wrongException() {
thrown.handleAssertionErrors();
thrown.expect(AssertionError.class);
throw new AssertionError("the expected assertion error");
}
Expand All @@ -298,7 +297,6 @@ public static class DontThrowAssertionErrorButExpectOne {

@Test
public void assertionErrorExpectedButNonIsThrown() {
thrown.handleAssertionErrors();
thrown.expect(AssertionError.class);
}
}
Expand Down Expand Up @@ -333,7 +331,6 @@ public static class ThrowExpectedAssumptionViolatedException {

@Test
public void throwExpectAssumptionViolatedException() {
thrown.handleAssumptionViolatedExceptions();
thrown.expect(AssumptionViolatedException.class);
throw new AssumptionViolatedException("");
}
Expand Down

0 comments on commit 5085af7

Please sign in to comment.