Skip to content

Commit a71f8c1

Browse files
author
David Saff
committed
Merge pull request #750 from stefanbirkner/expected-exception
Allow expectation as parameter of custom message.
2 parents 0f623cd + f40f106 commit a71f8c1

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.junit.rules;
22

3+
import static java.lang.String.format;
34
import static org.hamcrest.CoreMatchers.containsString;
45
import static org.hamcrest.CoreMatchers.instanceOf;
56
import static org.junit.Assert.assertThat;
@@ -93,8 +94,11 @@
9394
* <h3>Missing Exceptions</h3>
9495
* <p>
9596
* By default missing exceptions are reported with an error message
96-
* like "Expected test to throw foo.". You can configure a different
97-
* message by means of {@link #reportMissingExceptionWithMessage(String)}.
97+
* like "Expected test to throw an instance of foo". You can configure a different
98+
* message by means of {@link #reportMissingExceptionWithMessage(String)}. You
99+
* can use a {@code %s} placeholder for the description of the expected
100+
* exception. E.g. "Test doesn't throw %s." will fail with the error message
101+
* "Test doesn't throw an instance of foo.".
98102
*
99103
* @since 4.7
100104
*/
@@ -109,7 +113,7 @@ public static ExpectedException none() {
109113

110114
private final ExpectedExceptionMatcherBuilder fMatcherBuilder = new ExpectedExceptionMatcherBuilder();
111115

112-
private String missingExceptionMessage;
116+
private String missingExceptionMessage= "Expected test to throw %s";
113117

114118
private ExpectedException() {
115119
}
@@ -136,7 +140,11 @@ public ExpectedException handleAssumptionViolatedExceptions() {
136140

137141
/**
138142
* Specifies the failure message for tests that are expected to throw
139-
* an exception but do not throw any.
143+
* an exception but do not throw any. You can use a {@code %s} placeholder for
144+
* the description of the expected exception. E.g. "Test doesn't throw %s."
145+
* will fail with the error message
146+
* "Test doesn't throw an instance of foo.".
147+
*
140148
* @param message exception detail message
141149
* @return the rule itself
142150
*/
@@ -255,15 +263,7 @@ private void failDueToMissingException() throws AssertionError {
255263
}
256264

257265
private String missingExceptionMessage() {
258-
if (isMissingExceptionMessageEmpty()) {
259-
String expectation = StringDescription.toString(fMatcherBuilder.build());
260-
return "Expected test to throw " + expectation;
261-
} else {
262-
return missingExceptionMessage;
263-
}
264-
}
265-
266-
private boolean isMissingExceptionMessageEmpty() {
267-
return missingExceptionMessage == null || missingExceptionMessage.length() == 0;
266+
String expectation= StringDescription.toString(fMatcherBuilder.build());
267+
return format(missingExceptionMessage, expectation);
268268
}
269269
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public void describeTo(org.hamcrest.Description description) {
3131
description.appendValue(numberOfFailures);
3232
description.appendText(" failures");
3333
}
34+
35+
@Override
36+
protected void describeMismatchSafely(EventCollector item,
37+
org.hamcrest.Description description) {
38+
description.appendValue(item.fFailures.size());
39+
description.appendText(" failures");
40+
}
3441
};
3542
}
3643

@@ -84,6 +91,24 @@ public void describeTo(org.hamcrest.Description description) {
8491
description.appendText("has single failure with message ");
8592
messageMatcher.describeTo(description);
8693
}
94+
95+
@Override
96+
protected void describeMismatchSafely(EventCollector item,
97+
org.hamcrest.Description description) {
98+
description.appendText("was ");
99+
hasSingleFailure().describeMismatch(item, description);
100+
description.appendText(": ");
101+
boolean first= true;
102+
for (Failure f : item.fFailures) {
103+
if (!first) {
104+
description.appendText(" ,");
105+
}
106+
description.appendText("'");
107+
description.appendText(f.getMessage());
108+
description.appendText("'");
109+
first= false;
110+
}
111+
}
87112
};
88113
}
89114

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

+36-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class ExpectedExceptionTest {
3333
private static final String ARBITRARY_MESSAGE = "arbitrary message";
3434

35-
@Parameters
35+
@Parameters(name= "{0}")
3636
public static Collection<Object[]> testsWithEventMatcher() {
3737
return asList(new Object[][]{
3838
{EmptyTestExpectingNoException.class, everyTestRunSuccessful()},
@@ -78,7 +78,16 @@ public static Collection<Object[]> testsWithEventMatcher() {
7878
containsString("cause was <java.lang.NullPointerException: an unexpected cause>"),
7979
containsString("Stacktrace was: java.lang.IllegalArgumentException: Ack!"),
8080
containsString("Caused by: java.lang.NullPointerException: an unexpected cause")))},
81-
{ CustomMessageWithoutExpectedException.class, hasSingleFailureWithMessage(ARBITRARY_MESSAGE) }
81+
{
82+
UseNoCustomMessage.class,
83+
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.IllegalArgumentException") },
84+
{
85+
UseCustomMessageWithoutPlaceHolder.class,
86+
hasSingleFailureWithMessage(ARBITRARY_MESSAGE) },
87+
{
88+
UseCustomMessageWithPlaceHolder.class,
89+
hasSingleFailureWithMessage(ARBITRARY_MESSAGE
90+
+ " - an instance of java.lang.IllegalArgumentException") }
8291
});
8392
}
8493

@@ -321,11 +330,35 @@ public void throwWithCause() {
321330
}
322331
}
323332

324-
public static class CustomMessageWithoutExpectedException {
333+
public static class UseNoCustomMessage {
334+
335+
@Rule
336+
public ExpectedException thrown= ExpectedException.none();
337+
338+
@Test
339+
public void noThrow() {
340+
thrown.expect(IllegalArgumentException.class);
341+
}
342+
}
343+
344+
public static class UseCustomMessageWithPlaceHolder {
325345

326346
@Rule
327347
public ExpectedException thrown = ExpectedException.none();
328348

349+
@Test
350+
public void noThrow() {
351+
thrown.expect(IllegalArgumentException.class);
352+
thrown.reportMissingExceptionWithMessage(ARBITRARY_MESSAGE
353+
+ " - %s");
354+
}
355+
}
356+
357+
public static class UseCustomMessageWithoutPlaceHolder {
358+
359+
@Rule
360+
public ExpectedException thrown= ExpectedException.none();
361+
329362
@Test
330363
public void noThrow() {
331364
thrown.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)