1 package org.junit.tests.assertion;
2
3 import static org.hamcrest.CoreMatchers.equalTo;
4 import static org.junit.Assert.assertSame;
5 import static org.junit.Assert.assertThat;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8
9 import java.lang.annotation.AnnotationFormatError;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.junit.Test;
15 import org.junit.runners.model.MultipleFailureException;
16
17
18
19
20
21
22 public class MultipleFailureExceptionTest {
23
24 @Test
25 public void assertEmptyDoesNotThrowForEmptyList() throws Exception {
26 MultipleFailureException.assertEmpty(Collections.<Throwable>emptyList());
27 }
28
29 @Test
30 public void assertEmptyRethrowsSingleRuntimeException() throws Exception {
31 Throwable exception= new ExpectedException("pesto");
32 List<Throwable> errors= Collections.singletonList(exception);
33 try {
34 MultipleFailureException.assertEmpty(errors);
35 fail();
36 } catch (ExpectedException e) {
37 assertSame(e, exception);
38 }
39 }
40
41 @Test
42 public void assertEmptyRethrowsSingleError() throws Exception {
43 Throwable exception= new AnnotationFormatError("changeo");
44 List<Throwable> errors= Collections.singletonList(exception);
45 try {
46 MultipleFailureException.assertEmpty(errors);
47 fail();
48 } catch (AnnotationFormatError e) {
49 assertSame(e, exception);
50 }
51 }
52
53 @Test
54 public void assertEmptyThrowsMutipleFailureExceptionForManyThrowables() throws Exception {
55 List<Throwable> errors = new ArrayList<Throwable>();
56 errors.add(new ExpectedException("basil"));
57 errors.add(new RuntimeException("garlic"));
58
59 try {
60 MultipleFailureException.assertEmpty(errors);
61 fail();
62 } catch (MultipleFailureException expected) {
63 assertThat(expected.getFailures(), equalTo(errors));
64 assertTrue(expected.getMessage().startsWith("There were 2 errors:\n"));
65 assertTrue(expected.getMessage().contains("ExpectedException(basil)\n"));
66 assertTrue(expected.getMessage().contains("RuntimeException(garlic)"));
67 }
68 }
69
70
71 private static class ExpectedException extends RuntimeException {
72 private static final long serialVersionUID = 1L;
73
74 public ExpectedException(String message) {
75 super(message);
76 }
77 }
78 }