Skip to content

Fix for issue #687: handle AssertionErrors and AssumptionViolatedExceptions by default again #711

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

Closed
Show file tree
Hide file tree
Changes from all 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
26 changes: 18 additions & 8 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public static ExpectedException none() {

private final ExpectedExceptionMatcherBuilder fMatcherBuilder = new ExpectedExceptionMatcherBuilder();

private boolean handleAssumptionViolatedExceptions = false;
private boolean handleAssumptionViolatedExceptions = true;

private boolean handleAssertionErrors = true;

private boolean handleAssertionErrors = false;

private String missingExceptionMessage;

private ExpectedException() {
Expand All @@ -107,13 +107,23 @@ public ExpectedException handleAssertionErrors() {
return this;
}

public ExpectedException doNotHandleAssertionErrors() {
handleAssertionErrors = false;
return this;
}

public ExpectedException handleAssumptionViolatedExceptions() {
handleAssumptionViolatedExceptions = true;
return this;
}


public ExpectedException doNotHandleAssumptionViolatedExceptions() {
handleAssumptionViolatedExceptions = false;
return this;
}

/**
* Specifies the failure message for tests that are expected to throw
* Specifies the failure message for tests that are expected to throw
* an exception but do not throw any.
* @param message exception detail message
* @return self
Expand Down Expand Up @@ -215,16 +225,16 @@ private void handleException(Throwable e) throws Throwable {
private void failDueToMissingException() throws AssertionError {
fail(missingExceptionMessage());
}

private String missingExceptionMessage() {
if (isMissingExceptionMessageEmpty()) {
String expectation = StringDescription.toString(fMatcherBuilder.build());
return "Expected test to throw " + expectation;
} else {
return missingExceptionMessage;
}
}
}

private boolean isMissingExceptionMessageEmpty() {
return missingExceptionMessage == null || missingExceptionMessage.length() == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,25 @@ public static Collection<Object[]> testsWithEventMatcher() {
{ExpectsMatcher.class, everyTestRunSuccessful()},
{ThrowExpectedAssumptionViolatedException.class,
everyTestRunSuccessful()},
{HandlesAssumptionViolatedExceptionByDefault.class,
everyTestRunSuccessful()},
{ThrowAssumptionViolatedExceptionButExpectOtherType.class,
hasSingleFailure()},
{
ThrowAssumptionViolatedExceptionButExpectOtherType.class,
hasSingleFailureWithMessage(containsString("Stacktrace was: org.junit.internal.AssumptionViolatedException"))},
{ViolateAssumptionAndExpectException.class,
hasSingleAssumptionFailure()},
{ViolateAssumptionAndExpectException.class, hasSingleAssumptionFailure()},
{HandleAssumptionViolatedExceptionAndExpectException.class, hasSingleFailure()},
{ThrowExpectedAssertionError.class, everyTestRunSuccessful()},
{
DontThrowAssertionErrorButExpectOne.class,
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")},
{
ThrowUnexpectedAssertionError.class,
hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))},
{FailAndDontHandleAssertinErrors.class,
{HandlesAssertionErrorsByDefault.class,
everyTestRunSuccessful()},
{FailAndDontHandleAssertionErrors.class,
hasSingleFailureWithMessage(ARBITRARY_MESSAGE)},
{
ExpectsMultipleMatchers.class,
Expand Down Expand Up @@ -257,13 +261,25 @@ public void throwsMore() {
}
}

public static class FailAndDontHandleAssertinErrors {
public static class HandlesAssertionErrorsByDefault {
@Rule
public ExpectedException thrown = none();

@Test
public void noNeedToCallHandleAssertionErrors() {
thrown.expect(AssertionError.class);
throw new AssertionError();
}
}

public static class FailAndDontHandleAssertionErrors {
@Rule
public ExpectedException thrown = none();

@Test
public void violatedAssumption() {
public void expectExceptionButThrowAssertionErrorNotHandled() {
thrown.expect(IllegalArgumentException.class);
thrown.doNotHandleAssertionErrors();
fail(ARBITRARY_MESSAGE);
}
}
Expand All @@ -285,8 +301,7 @@ public static class ThrowExpectedAssertionError {
public ExpectedException thrown = none();

@Test
public void wrongException() {
thrown.handleAssertionErrors();
public void passes() {
thrown.expect(AssertionError.class);
throw new AssertionError("the expected assertion error");
}
Expand All @@ -308,8 +323,20 @@ public static class ViolateAssumptionAndExpectException {
public ExpectedException thrown = none();

@Test
public void violatedAssumption() {
// expect an exception, which is not an AssumptionViolatedException
public void expectExceptionWhichIsNotAnAssumptionViolatedException() {
thrown.doNotHandleAssumptionViolatedExceptions();
thrown.expect(NullPointerException.class);
assumeTrue(false);
}
}

public static class HandleAssumptionViolatedExceptionAndExpectException {
@Rule
public ExpectedException thrown = none();

@Test
public void expectExceptionWhichIsNotAnAssumptionViolatedException() {
thrown.handleAssumptionViolatedExceptions();
thrown.expect(NullPointerException.class);
assumeTrue(false);
}
Expand Down Expand Up @@ -339,6 +366,17 @@ public void throwExpectAssumptionViolatedException() {
}
}

public static class HandlesAssumptionViolatedExceptionByDefault {
@Rule
public ExpectedException thrown = none();

@Test
public void noNeedToCallHandleAssumptionViolatedExceptions() {
thrown.expect(AssumptionViolatedException.class);
throw new AssumptionViolatedException("");
}
}

public static class ThrowExceptionWithMatchingCause {
@Rule
public ExpectedException thrown = none();
Expand Down Expand Up @@ -383,7 +421,7 @@ public void throwWithCause() {
throw new IllegalArgumentException("Ack!", new NullPointerException("an unexpected cause"));
}
}

public static class CustomMessageWithoutExpectedException {

@Rule
Expand All @@ -395,4 +433,4 @@ public void noThrow() {
thrown.reportMissingExceptionWithMessage(ARBITRARY_MESSAGE);
}
}
}
}