Skip to content

Commit ad0f505

Browse files
author
David Saff
committed
Merge pull request #583 from UrsMetz/expected-exception-fail-when-assertion-error-expected-but-not-thown
ExpectedException doesn't fail when an AssertionError is expected but not thrown
2 parents ef6ee7f + 04218be commit ad0f505

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,22 @@ public ExpectedExceptionStatement(Statement base) {
179179
public void evaluate() throws Throwable {
180180
try {
181181
fNext.evaluate();
182-
if (fMatcherBuilder.expectsThrowable()) {
183-
failDueToMissingException();
184-
}
185182
} catch (AssumptionViolatedException e) {
186183
optionallyHandleException(e, handleAssumptionViolatedExceptions);
184+
return;
187185
} catch (AssertionError e) {
188186
optionallyHandleException(e, handleAssertionErrors);
187+
return;
189188
} catch (Throwable e) {
190189
handleException(e);
190+
return;
191+
}
192+
if (fMatcherBuilder.expectsThrowable()) {
193+
failDueToMissingException();
191194
}
192195
}
193196
}
194197

195-
private void failDueToMissingException() throws AssertionError {
196-
fail(missingExceptionMessage());
197-
}
198-
199198
private void optionallyHandleException(Throwable e, boolean handleException)
200199
throws Throwable {
201200
if (handleException) {
@@ -212,6 +211,10 @@ private void handleException(Throwable e) throws Throwable {
212211
throw e;
213212
}
214213
}
214+
215+
private void failDueToMissingException() throws AssertionError {
216+
fail(missingExceptionMessage());
217+
}
215218

216219
private String missingExceptionMessage() {
217220
if (isMissingExceptionMessageEmpty()) {

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

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public static Collection<Object[]> testsWithEventMatcher() {
7171
{ViolateAssumptionAndExpectException.class,
7272
hasSingleAssumptionFailure()},
7373
{ThrowExpectedAssertionError.class, everyTestRunSuccessful()},
74+
{
75+
DontThrowAssertionErrorButExpectOne.class,
76+
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")},
7477
{
7578
ThrowUnexpectedAssertionError.class,
7679
hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))},
@@ -289,6 +292,17 @@ public void wrongException() {
289292
}
290293
}
291294

295+
public static class DontThrowAssertionErrorButExpectOne {
296+
@Rule
297+
public ExpectedException thrown = none();
298+
299+
@Test
300+
public void assertionErrorExpectedButNonIsThrown() {
301+
thrown.handleAssertionErrors();
302+
thrown.expect(AssertionError.class);
303+
}
304+
}
305+
292306
public static class ViolateAssumptionAndExpectException {
293307
@Rule
294308
public ExpectedException thrown = none();

0 commit comments

Comments
 (0)