Skip to content

Commit

Permalink
Implementation for issue junit-team#144
Browse files Browse the repository at this point in the history
Added additional public method, setMissingExceptionMessage, to allow a
user to specify a custom message that would be thrown in the event a
test did not throw the expected exception. If one is not provided the
class falls back to the previous behavior.
  • Loading branch information
Corey Vaillancourt committed Nov 10, 2012
1 parent 94aa692 commit 8f0902e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ MaxCore.max
*.iml
*.iws
out
java.hprof.txt
java.hprof.txt
.gitattributes
29 changes: 27 additions & 2 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public static ExpectedException none() {
private boolean handleAssumptionViolatedExceptions = false;

private boolean handleAssertionErrors = false;

private String missingExceptionMessage;

private ExpectedException() {
}
Expand All @@ -109,6 +111,17 @@ public ExpectedException handleAssumptionViolatedExceptions() {
handleAssumptionViolatedExceptions = true;
return this;
}

/**
* Specifies the detail message for an exception to be thrown if the test does
* not throw the expected exception.
* @param providedMessage exception detail message
* @return self
*/
public ExpectedException setMissingExceptionMessage(String providedMessage) {
missingExceptionMessage = providedMessage;
return this;
}

public Statement apply(Statement base,
org.junit.runner.Description description) {
Expand Down Expand Up @@ -180,8 +193,16 @@ public void evaluate() throws Throwable {
}

private void failDueToMissingException() throws AssertionError {
String expectation = StringDescription.toString(fMatcherBuilder.build());
fail("Expected test to throw " + expectation);
String failureMessage;

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

fail(failureMessage);
}

private void optionallyHandleException(Throwable e, boolean handleException)
Expand All @@ -200,4 +221,8 @@ private void handleException(Throwable e) throws Throwable {
throw e;
}
}

private boolean isMissingExceptionMessageEmpty() {
return missingExceptionMessage == null || missingExceptionMessage.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public static Collection<Object[]> testsWithEventMatcher() {
containsString("exception with cause is <java.lang.NullPointerException: expected cause>"),
containsString("cause was <java.lang.NullPointerException: an unexpected cause>"),
containsString("Stacktrace was: java.lang.IllegalArgumentException: Ack!"),
containsString("Caused by: java.lang.NullPointerException: an unexpected cause")))}
containsString("Caused by: java.lang.NullPointerException: an unexpected cause")))},
{ CustomMessageWithoutExpectedException.class, hasSingleFailureWithMessage(ARBITRARY_MESSAGE) }
});
}

Expand Down Expand Up @@ -368,4 +369,16 @@ public void throwWithCause() {
throw new IllegalArgumentException("Ack!", new NullPointerException("an unexpected cause"));
}
}

public static class CustomMessageWithoutExpectedException {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void throwWithCause() {
thrown.expect(IllegalArgumentException.class);
thrown.setMissingExceptionMessage(ARBITRARY_MESSAGE);
}
}
}

0 comments on commit 8f0902e

Please sign in to comment.