summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/assertion/MultipleFailureExceptionTest.java
blob: 1ddbcea53414b42de6d95d59442e16c42d26cb4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2010 Google Inc. All Rights Reserved.

package org.junit.tests.assertion;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.junit.runners.model.MultipleFailureException;

/**
 * Tests for {@link org.junit.runners.model.MultipleFailureException}
 *
 * @author kcooney@google.com (Kevin Cooney)
 */
public class MultipleFailureExceptionTest {

	@Test
	public void assertEmptyDoesNotThrowForEmptyList() throws Throwable {
		MultipleFailureException.assertEmpty(Collections.<Throwable>emptyList());
	}
	
	@Test(expected=ExpectedException.class)
	public void assertEmptyRethrowsSingleThrowable() throws Throwable {
		MultipleFailureException.assertEmpty(
				Collections.<Throwable>singletonList(new ExpectedException("pesto")));
	}
	
	@Test
	public void assertEmptyThrowsMutipleFailureExceptionForManyThrowables() throws Throwable {
		List<Throwable> errors = new ArrayList<Throwable>();
		errors.add(new ExpectedException("basil"));
		errors.add(new RuntimeException("garlic"));
		
		try {
			MultipleFailureException.assertEmpty(errors);
			fail();
		} catch (MultipleFailureException expected) {
			assertThat(expected.getFailures(), equalTo(errors));
			assertTrue(expected.getMessage().startsWith("There were 2 errors:\n"));
			assertTrue(expected.getMessage().contains("ExpectedException(basil)\n"));
			assertTrue(expected.getMessage().contains("RuntimeException(garlic)"));
		}
	}


	private static class ExpectedException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		public ExpectedException(String message) {
			super(message);
		}
	}
}