Skip to content

Commit 8e4aeef

Browse files
committed
Add tests.
1 parent 8c14a66 commit 8e4aeef

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

src/test/java/org/junit/tests/AllTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.tests.manipulation.AllManipulationTests;
2121
import org.junit.tests.running.AllRunningTests;
2222
import org.junit.tests.validation.AllValidationTests;
23+
import org.junit.testsupport.AllTestSupportTests;
2324
import org.junit.validator.AllValidatorTests;
2425

2526
@RunWith(Suite.class)
@@ -37,6 +38,7 @@
3738
AllRunnerTests.class,
3839
AllRunningTests.class,
3940
AllSamplesTests.class,
41+
AllTestSupportTests.class,
4042
AllValidationTests.class,
4143
AllValidatorTests.class,
4244
AssumptionViolatedExceptionTest.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.junit.testsupport;
2+
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
6+
@RunWith(Suite.class)
7+
@Suite.SuiteClasses({
8+
EventCollectorMatchersTest.class,
9+
EventCollectorTest.class
10+
})
11+
public class AllTestSupportTests {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.junit.testsupport;
2+
3+
import org.hamcrest.Matcher;
4+
import org.junit.Test;
5+
import org.junit.runner.Description;
6+
import org.junit.runner.Result;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runner.notification.Failure;
9+
import org.junit.runners.Parameterized;
10+
11+
import java.util.Arrays;
12+
13+
import static java.util.Arrays.asList;
14+
import static org.hamcrest.CoreMatchers.not;
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.junit.runners.Parameterized.Parameter;
17+
import static org.junit.runners.Parameterized.Parameters;
18+
import static org.junit.testsupport.EventCollectorMatchers.everyTestRunSuccessful;
19+
import static org.junit.testsupport.EventCollectorMatchers.hasNoAssumptionFailure;
20+
import static org.junit.testsupport.EventCollectorMatchers.hasSingleAssumptionFailure;
21+
22+
@RunWith(Parameterized.class)
23+
public class EventCollectorMatchersTest {
24+
private static final Description DUMMY_DESCRIPTION = Description.EMPTY;
25+
26+
private static final Failure DUMMY_FAILURE = new Failure(null, new RuntimeException("dummy message"));
27+
28+
private static final Result DUMMY_RESULT = new Result();
29+
30+
private static final EventCollector COLLECTOR_WITH_NO_EVENTS = new EventCollector();
31+
32+
private static final EventCollector COLLECTOR_WITH_SINGLE_FAILURE = new EventCollector() {{
33+
testFailure(DUMMY_FAILURE);
34+
}};
35+
36+
private static final EventCollector COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE = new EventCollector() {{
37+
testAssumptionFailure(DUMMY_FAILURE);
38+
}};
39+
40+
@Parameters(name = "{0}")
41+
public static Iterable<Object[]> data() {
42+
return asList(
43+
new Object[] {"everyTestRunSuccessful() matches if no failures are reported", COLLECTOR_WITH_NO_EVENTS, everyTestRunSuccessful()},
44+
new Object[] {"everyTestRunSuccessful() does not match if failure is reported", COLLECTOR_WITH_SINGLE_FAILURE, not(everyTestRunSuccessful())},
45+
new Object[] {"everyTestRunSuccessful() does not match if assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, not(everyTestRunSuccessful())},
46+
new Object[] {"hasNoAssumptionFailure() matches if no assumption failure is reported", COLLECTOR_WITH_NO_EVENTS, hasNoAssumptionFailure()},
47+
new Object[] {"hasNoAssumptionFailure() does not match if assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, not(hasNoAssumptionFailure())},
48+
new Object[] {"hasSingleAssumptionFailure() matches if single assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, hasSingleAssumptionFailure()},
49+
new Object[] {"hasSingleAssumptionFailure() does not match if no assumption failure is reported", COLLECTOR_WITH_NO_EVENTS, not(hasSingleAssumptionFailure())});
50+
}
51+
52+
@Parameter(0)
53+
public String testName; //must be assigned. Otherwise the Parameterized runner fails.
54+
55+
@Parameter(1)
56+
public EventCollector collector;
57+
58+
@Parameter(2)
59+
public Matcher<EventCollector> matcher;
60+
61+
@Test
62+
public void matchesCollector() {
63+
assertThat(collector, matcher);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.junit.testsupport;
2+
3+
import static java.util.Collections.singletonList;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.rules.ExpectedException.none;
6+
7+
import java.util.List;
8+
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.ExpectedException;
12+
import org.junit.runner.Description;
13+
import org.junit.runner.Result;
14+
import org.junit.runner.notification.Failure;
15+
16+
public class EventCollectorTest {
17+
private static final Description DUMMY_DESCRIPTION = Description.EMPTY;
18+
private static final Failure DUMMY_FAILURE = new Failure(null, null);
19+
private static final Result DUMMY_RESULT = new Result();
20+
21+
@Rule
22+
public final ExpectedException thrown = none();
23+
24+
private final EventCollector collector = new EventCollector();
25+
26+
@Test
27+
public void collectsTestRunsStarted() {
28+
collector.testRunStarted(DUMMY_DESCRIPTION);
29+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestRunsStarted());
30+
}
31+
32+
@Test
33+
public void returnsUnmodifiableListOfTestRunsStarted() {
34+
assertNoDescriptionCanBeAddedToList(collector.getTestRunsStarted());
35+
}
36+
37+
@Test
38+
public void collectsTestRunsFinished() {
39+
collector.testRunFinished(DUMMY_RESULT);
40+
assertEquals(singletonList(DUMMY_RESULT), collector.getTestRunsFinished());
41+
}
42+
43+
@Test
44+
public void returnsUnmodifiableListOfTestRunsFinished() {
45+
assertNoResultCanBeAddedToList(collector.getTestRunsFinished());
46+
}
47+
48+
@Test
49+
public void collectsTestSuitesStarted() {
50+
collector.testSuiteStarted(DUMMY_DESCRIPTION);
51+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestSuitesStarted());
52+
}
53+
54+
@Test
55+
public void returnsUnmodifiableListOfTestSuitesStarted() {
56+
assertNoDescriptionCanBeAddedToList(collector.getTestSuitesStarted());
57+
}
58+
59+
@Test
60+
public void collectsTestSuitesFinished() {
61+
collector.testSuiteFinished(DUMMY_DESCRIPTION);
62+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestSuitesFinished());
63+
}
64+
65+
@Test
66+
public void returnsUnmodifiableListOfTestSuitesFinished() {
67+
assertNoDescriptionCanBeAddedToList(collector.getTestSuitesFinished());
68+
}
69+
70+
@Test
71+
public void collectsTestsStarted() {
72+
collector.testStarted(DUMMY_DESCRIPTION);
73+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsStarted());
74+
}
75+
76+
@Test
77+
public void returnsUnmodifiableListOfTestsStarted() {
78+
assertNoDescriptionCanBeAddedToList(collector.getTestsStarted());
79+
}
80+
81+
@Test
82+
public void collectsTestsFinished() {
83+
collector.testFinished(DUMMY_DESCRIPTION);
84+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsFinished());
85+
}
86+
87+
@Test
88+
public void returnsUnmodifiableListOfTestsFinished() {
89+
assertNoDescriptionCanBeAddedToList(collector.getTestsFinished());
90+
}
91+
92+
@Test
93+
public void collectsFailures() {
94+
collector.testFailure(DUMMY_FAILURE);
95+
assertEquals(singletonList(DUMMY_FAILURE), collector.getFailures());
96+
}
97+
98+
@Test
99+
public void returnsUnmodifiableListOfFailures() {
100+
assertNoFailureCanBeAddedToList(collector.getFailures());
101+
}
102+
103+
@Test
104+
public void collectsAssumptionFailures() {
105+
collector.testAssumptionFailure(DUMMY_FAILURE);
106+
assertEquals(singletonList(DUMMY_FAILURE), collector.getAssumptionFailures());
107+
}
108+
109+
@Test
110+
public void returnsUnmodifiableListOfAssumptionFailures() {
111+
assertNoFailureCanBeAddedToList(collector.getAssumptionFailures());
112+
}
113+
114+
@Test
115+
public void collectsTestsIgnored() {
116+
collector.testIgnored(DUMMY_DESCRIPTION);
117+
assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsIgnored());
118+
}
119+
120+
@Test
121+
public void returnsUnmodifiableListOfTestsIgnored() {
122+
assertNoDescriptionCanBeAddedToList(collector.getTestsIgnored());
123+
}
124+
125+
private void assertNoDescriptionCanBeAddedToList(List<Description> list) {
126+
thrown.expect(Exception.class);
127+
list.add(DUMMY_DESCRIPTION);
128+
}
129+
130+
private void assertNoFailureCanBeAddedToList(List<Failure> list) {
131+
thrown.expect(Exception.class);
132+
list.add(DUMMY_FAILURE);
133+
}
134+
135+
private void assertNoResultCanBeAddedToList(List<Result> list) {
136+
thrown.expect(Exception.class);
137+
list.add(DUMMY_RESULT);
138+
}
139+
}

0 commit comments

Comments
 (0)