File tree 2 files changed +24
-7
lines changed
main/java/org/junit/rules
test/java/org/junit/tests/experimental/rules
2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -179,23 +179,22 @@ public ExpectedExceptionStatement(Statement base) {
179
179
public void evaluate () throws Throwable {
180
180
try {
181
181
fNext .evaluate ();
182
- if (fMatcherBuilder .expectsThrowable ()) {
183
- failDueToMissingException ();
184
- }
185
182
} catch (AssumptionViolatedException e ) {
186
183
optionallyHandleException (e , handleAssumptionViolatedExceptions );
184
+ return ;
187
185
} catch (AssertionError e ) {
188
186
optionallyHandleException (e , handleAssertionErrors );
187
+ return ;
189
188
} catch (Throwable e ) {
190
189
handleException (e );
190
+ return ;
191
+ }
192
+ if (fMatcherBuilder .expectsThrowable ()) {
193
+ failDueToMissingException ();
191
194
}
192
195
}
193
196
}
194
197
195
- private void failDueToMissingException () throws AssertionError {
196
- fail (missingExceptionMessage ());
197
- }
198
-
199
198
private void optionallyHandleException (Throwable e , boolean handleException )
200
199
throws Throwable {
201
200
if (handleException ) {
@@ -212,6 +211,10 @@ private void handleException(Throwable e) throws Throwable {
212
211
throw e ;
213
212
}
214
213
}
214
+
215
+ private void failDueToMissingException () throws AssertionError {
216
+ fail (missingExceptionMessage ());
217
+ }
215
218
216
219
private String missingExceptionMessage () {
217
220
if (isMissingExceptionMessageEmpty ()) {
Original file line number Diff line number Diff line change @@ -71,6 +71,9 @@ public static Collection<Object[]> testsWithEventMatcher() {
71
71
{ViolateAssumptionAndExpectException .class ,
72
72
hasSingleAssumptionFailure ()},
73
73
{ThrowExpectedAssertionError .class , everyTestRunSuccessful ()},
74
+ {
75
+ DontThrowAssertionErrorButExpectOne .class ,
76
+ hasSingleFailureWithMessage ("Expected test to throw an instance of java.lang.AssertionError" )},
74
77
{
75
78
ThrowUnexpectedAssertionError .class ,
76
79
hasSingleFailureWithMessage (startsWith ("\n Expected: an instance of java.lang.NullPointerException" ))},
@@ -289,6 +292,17 @@ public void wrongException() {
289
292
}
290
293
}
291
294
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
+
292
306
public static class ViolateAssumptionAndExpectException {
293
307
@ Rule
294
308
public ExpectedException thrown = none ();
You can’t perform that action at this time.
0 commit comments