Skip to content

Commit c4e761e

Browse files
committed
Handle expected AssertionError and AssumptionViolatedException.
Fixes junit-team#687.
1 parent 1028d1c commit c4e761e

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/main/java/org/junit/rules/ExpectedException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,16 @@ private void optionallyHandleException(Throwable e, boolean handleException)
199199
throws Throwable {
200200
if (handleException) {
201201
handleException(e);
202-
} else {
202+
} else if (!isExpectedException(e)) {
203203
throw e;
204204
}
205205
}
206206

207+
private boolean isExpectedException(Throwable e) {
208+
return fMatcherBuilder.expectsThrowable()
209+
&& fMatcherBuilder.build().matches(e);
210+
}
211+
207212
private void handleException(Throwable e) throws Throwable {
208213
if (fMatcherBuilder.expectsThrowable()) {
209214
assertThat(e, fMatcherBuilder.build());

src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ public static class ThrowExpectedAssertionError {
286286

287287
@Test
288288
public void wrongException() {
289-
thrown.handleAssertionErrors();
290289
thrown.expect(AssertionError.class);
291290
throw new AssertionError("the expected assertion error");
292291
}
@@ -298,7 +297,6 @@ public static class DontThrowAssertionErrorButExpectOne {
298297

299298
@Test
300299
public void assertionErrorExpectedButNonIsThrown() {
301-
thrown.handleAssertionErrors();
302300
thrown.expect(AssertionError.class);
303301
}
304302
}
@@ -333,7 +331,6 @@ public static class ThrowExpectedAssumptionViolatedException {
333331

334332
@Test
335333
public void throwExpectAssumptionViolatedException() {
336-
thrown.handleAssumptionViolatedExceptions();
337334
thrown.expect(AssumptionViolatedException.class);
338335
throw new AssumptionViolatedException("");
339336
}

0 commit comments

Comments
 (0)