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

ExpectedException doesn't fail when an AssertionError is expected but not thrown #583

Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 10 additions & 7 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,22 @@ public ExpectedExceptionStatement(Statement base) {
public void evaluate() throws Throwable {
try {
fNext.evaluate();
if (fMatcherBuilder.expectsThrowable()) {
failDueToMissingException();
}
} catch (AssumptionViolatedException e) {
optionallyHandleException(e, handleAssumptionViolatedExceptions);
return;
} catch (AssertionError e) {
optionallyHandleException(e, handleAssertionErrors);
return;
} catch (Throwable e) {
handleException(e);
return;
}
if (fMatcherBuilder.expectsThrowable()) {
failDueToMissingException();
}
}
}

private void failDueToMissingException() throws AssertionError {
fail(missingExceptionMessage());
}

private void optionallyHandleException(Throwable e, boolean handleException)
throws Throwable {
if (handleException) {
Expand All @@ -212,6 +211,10 @@ private void handleException(Throwable e) throws Throwable {
throw e;
}
}

private void failDueToMissingException() throws AssertionError {
fail(missingExceptionMessage());
}

private String missingExceptionMessage() {
if (isMissingExceptionMessageEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public static Collection<Object[]> testsWithEventMatcher() {
{ViolateAssumptionAndExpectException.class,
hasSingleAssumptionFailure()},
{ThrowExpectedAssertionError.class, everyTestRunSuccessful()},
{
DontThrowAssertionErrorButExpectOne.class,
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")},
Copy link
Member

Choose a reason for hiding this comment

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

The above two lines should be indented by one additional tab.

{
ThrowUnexpectedAssertionError.class,
hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))},
Expand Down Expand Up @@ -288,6 +291,16 @@ public void wrongException() {
throw new AssertionError("the expected assertion error");
}
}
public static class DontThrowAssertionErrorButExpectOne {
Copy link
Member

Choose a reason for hiding this comment

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

Please add an empty line above the class definition.

@Rule
public ExpectedException thrown = none();

@Test
public void assertionErrorExpectedButNonIsThrown() {
thrown.handleAssertionErrors();
thrown.expect(AssertionError.class);
}
}

public static class ViolateAssumptionAndExpectException {
@Rule
Expand Down