View Javadoc
1   package org.junit.tests.experimental.rules;
2   
3   import static java.util.Arrays.asList;
4   import static org.hamcrest.CoreMatchers.any;
5   import static org.hamcrest.CoreMatchers.containsString;
6   import static org.hamcrest.CoreMatchers.is;
7   import static org.hamcrest.CoreMatchers.nullValue;
8   import static org.hamcrest.CoreMatchers.startsWith;
9   import static org.hamcrest.core.IsEqual.equalTo;
10  import static org.junit.Assert.assertThat;
11  import static org.junit.Assert.fail;
12  import static org.junit.Assume.assumeTrue;
13  import static org.junit.rules.ExpectedException.none;
14  import static org.junit.tests.experimental.rules.EventCollector.everyTestRunSuccessful;
15  import static org.junit.tests.experimental.rules.EventCollector.hasSingleAssumptionFailure;
16  import static org.junit.tests.experimental.rules.EventCollector.hasSingleFailure;
17  import static org.junit.tests.experimental.rules.EventCollector.hasSingleFailureWithMessage;
18  
19  import java.util.Collection;
20  
21  import org.hamcrest.CoreMatchers;
22  import org.hamcrest.Matcher;
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.junit.rules.ExpectedException;
26  import org.junit.runner.JUnitCore;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  @RunWith(Parameterized.class)
32  public class ExpectedExceptionTest {
33      private static final String ARBITRARY_MESSAGE = "arbitrary message";
34  
35      @Parameters(name= "{0}")
36      public static Collection<Object[]> testsWithEventMatcher() {
37          return asList(new Object[][]{
38                  {EmptyTestExpectingNoException.class, everyTestRunSuccessful()},
39                  {ThrowExceptionWithExpectedType.class,
40                          everyTestRunSuccessful()},
41                  {ThrowExceptionWithExpectedPartOfMessage.class,
42                          everyTestRunSuccessful()},
43                  {
44                          ThrowExceptionWithWrongType.class,
45                          hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))},
46                  {
47                          HasWrongMessage.class,
48                          hasSingleFailureWithMessage(startsWith("\nExpected: exception with message a string containing \"expectedMessage\"\n"
49                                  + "     but: message was \"actualMessage\""))},
50                  {
51                          ThrowNoExceptionButExpectExceptionWithType.class,
52                          hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.NullPointerException")},
53                  {WronglyExpectsExceptionMessage.class, hasSingleFailure()},
54                  {ExpectsSubstring.class, everyTestRunSuccessful()},
55                  {
56                          ExpectsSubstringNullMessage.class,
57                          hasSingleFailureWithMessage(startsWith("\nExpected: exception with message a string containing \"anything!\""))},
58                  {ExpectsMessageMatcher.class, everyTestRunSuccessful()},
59                  {
60                          ExpectedMessageMatcherFails.class,
61                          hasSingleFailureWithMessage(startsWith("\nExpected: exception with message \"Wrong start\""))},
62                  {ExpectsMatcher.class, everyTestRunSuccessful()},
63                  {ExpectAssertionErrorWhichIsNotThrown.class, hasSingleFailure()},
64                  {FailedAssumptionAndExpectException.class,
65                          hasSingleAssumptionFailure()},
66                  {FailBeforeExpectingException.class,
67                          hasSingleFailureWithMessage(ARBITRARY_MESSAGE)},
68                  {
69                          ExpectsMultipleMatchers.class,
70                          hasSingleFailureWithMessage(startsWith("\nExpected: (an instance of java.lang.IllegalArgumentException and exception with message a string containing \"Ack!\")"))},
71                  {ThrowExceptionWithMatchingCause.class, everyTestRunSuccessful()},
72                  {ThrowExpectedNullCause.class, everyTestRunSuccessful()},
73                  {
74                          ThrowUnexpectedCause.class,
75                          hasSingleFailureWithMessage(CoreMatchers.<String>allOf(
76                                  startsWith("\nExpected: ("),
77                                  containsString("exception with cause is <java.lang.NullPointerException: expected cause>"),
78                                  containsString("cause was <java.lang.NullPointerException: an unexpected cause>"),
79                                  containsString("Stacktrace was: java.lang.IllegalArgumentException: Ack!"),
80                                  containsString("Caused by: java.lang.NullPointerException: an unexpected cause")))},
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") }
91          });
92      }
93  
94      private final Class<?> classUnderTest;
95  
96      private final Matcher<EventCollector> matcher;
97  
98      public ExpectedExceptionTest(Class<?> classUnderTest,
99              Matcher<EventCollector> matcher) {
100         this.classUnderTest = classUnderTest;
101         this.matcher = matcher;
102     }
103 
104     @Test
105     public void runTestAndVerifyResult() {
106         EventCollector collector = new EventCollector();
107         JUnitCore core = new JUnitCore();
108         core.addListener(collector);
109         core.run(classUnderTest);
110         assertThat(collector, matcher);
111     }
112 
113     public static class EmptyTestExpectingNoException {
114         @Rule
115         public ExpectedException thrown = none();
116 
117         @Test
118         public void throwsNothing() {
119         }
120     }
121 
122     public static class ThrowExceptionWithExpectedType {
123         @Rule
124         public ExpectedException thrown = none();
125 
126         @Test
127         public void throwsNullPointerException() {
128             thrown.expect(NullPointerException.class);
129             throw new NullPointerException();
130         }
131     }
132 
133     public static class ThrowExceptionWithExpectedPartOfMessage {
134         @Rule
135         public ExpectedException thrown = none();
136 
137         @Test
138         public void throwsNullPointerExceptionWithMessage() {
139             thrown.expect(NullPointerException.class);
140             thrown.expectMessage(ARBITRARY_MESSAGE);
141             throw new NullPointerException(ARBITRARY_MESSAGE + "something else");
142         }
143     }
144 
145     public static class ThrowExceptionWithWrongType {
146         @Rule
147         public ExpectedException thrown = none();
148 
149         @Test
150         public void throwsNullPointerException() {
151             thrown.expect(NullPointerException.class);
152             throw new IllegalArgumentException();
153         }
154     }
155 
156     public static class HasWrongMessage {
157         @Rule
158         public ExpectedException thrown = none();
159 
160         @Test
161         public void throwsNullPointerException() {
162             thrown.expectMessage("expectedMessage");
163             throw new IllegalArgumentException("actualMessage");
164         }
165     }
166 
167     public static class ThrowNoExceptionButExpectExceptionWithType {
168         @Rule
169         public ExpectedException thrown = none();
170 
171         @Test
172         public void doesntThrowNullPointerException() {
173             thrown.expect(NullPointerException.class);
174         }
175     }
176 
177     public static class WronglyExpectsExceptionMessage {
178         @Rule
179         public ExpectedException thrown = none();
180 
181         @Test
182         public void doesntThrowAnything() {
183             thrown.expectMessage("anything!");
184         }
185     }
186 
187     public static class ExpectsSubstring {
188         @Rule
189         public ExpectedException thrown = none();
190 
191         @Test
192         public void throwsMore() {
193             thrown.expectMessage("anything!");
194             throw new NullPointerException(
195                     "This could throw anything! (as long as it has the right substring)");
196         }
197     }
198 
199     public static class ExpectsSubstringNullMessage {
200         @Rule
201         public ExpectedException thrown = none();
202 
203         @Test
204         public void throwsMore() {
205             thrown.expectMessage("anything!");
206             throw new NullPointerException();
207         }
208     }
209 
210     public static class ExpectsMessageMatcher {
211         @Rule
212         public ExpectedException thrown = none();
213 
214         @Test
215         public void throwsMore() {
216             thrown.expectMessage(startsWith(ARBITRARY_MESSAGE));
217             throw new NullPointerException(ARBITRARY_MESSAGE + "!");
218         }
219     }
220 
221     public static class ExpectedMessageMatcherFails {
222         @Rule
223         public ExpectedException thrown = none();
224 
225         @Test
226         public void throwsMore() {
227             thrown.expectMessage(equalTo("Wrong start"));
228             throw new NullPointerException("Back!");
229         }
230     }
231 
232     public static class ExpectsMatcher {
233         @Rule
234         public ExpectedException thrown = none();
235 
236         @Test
237         public void throwsMore() {
238             thrown.expect(any(Exception.class));
239             throw new NullPointerException("Ack!");
240         }
241     }
242 
243     public static class ExpectsMultipleMatchers {
244         @Rule
245         public ExpectedException thrown = none();
246 
247         @Test
248         public void throwsMore() {
249             thrown.expect(IllegalArgumentException.class);
250             thrown.expectMessage("Ack!");
251             throw new NullPointerException("Ack!");
252         }
253     }
254 
255     //https://github.com/junit-team/junit/pull/583
256     public static class ExpectAssertionErrorWhichIsNotThrown {
257         @Rule
258         public ExpectedException thrown = none();
259 
260         @Test
261         public void fails() {
262             thrown.expect(AssertionError.class);
263         }
264     }
265 
266     public static class FailBeforeExpectingException {
267         @Rule
268         public ExpectedException thrown = none();
269 
270         @Test
271         public void fails() {
272             fail(ARBITRARY_MESSAGE);
273             thrown.expect(IllegalArgumentException.class);
274         }
275     }
276 
277     public static class FailedAssumptionAndExpectException {
278         @Rule
279         public ExpectedException thrown = none();
280 
281         @Test
282         public void failedAssumption() {
283             assumeTrue(false);
284             thrown.expect(NullPointerException.class);
285         }
286     }
287 
288     public static class ThrowExceptionWithMatchingCause {
289         @Rule
290         public ExpectedException thrown = none();
291 
292         @Test
293         public void throwExceptionWithMatchingCause() {
294             NullPointerException expectedCause = new NullPointerException("expected cause");
295 
296             thrown.expect(IllegalArgumentException.class);
297             thrown.expectMessage("Ack!");
298             thrown.expectCause(is(expectedCause));
299 
300             throw new IllegalArgumentException("Ack!", expectedCause);
301         }
302     }
303 
304     public static class ThrowExpectedNullCause {
305         @Rule
306         public ExpectedException thrown = none();
307 
308         @Test
309         public void throwExpectedNullCause() {
310             thrown.expect(IllegalArgumentException.class);
311             thrown.expectMessage("Ack!");
312             thrown.expectCause(nullValue(Throwable.class));
313 
314             throw new IllegalArgumentException("Ack!");
315         }
316     }
317 
318     public static class ThrowUnexpectedCause {
319 
320         @Rule
321         public ExpectedException thrown = ExpectedException.none();
322 
323         @Test
324         public void throwWithCause() {
325             thrown.expect(IllegalArgumentException.class);
326             thrown.expectMessage("Ack!");
327             thrown.expectCause(is(new NullPointerException("expected cause")));
328 
329             throw new IllegalArgumentException("Ack!", new NullPointerException("an unexpected cause"));
330         }
331     }
332     
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 {
345 
346         @Rule
347         public ExpectedException thrown = ExpectedException.none();
348 
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 
362         @Test
363         public void noThrow() {
364             thrown.expect(IllegalArgumentException.class);
365             thrown.reportMissingExceptionWithMessage(ARBITRARY_MESSAGE);
366         }
367     }
368 }