Skip to content

Commit 3eb5e8c

Browse files
committed
Add tests.
1 parent f37aabe commit 3eb5e8c

File tree

4 files changed

+202
-6
lines changed

4 files changed

+202
-6
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
import org.junit.tests.validation.BadlyFormedClassesTest;
105105
import org.junit.tests.validation.FailedConstructionTest;
106106
import org.junit.tests.validation.ValidationTest;
107+
import org.junit.testsupport.EventCollectorMatchersTest;
108+
import org.junit.testsupport.EventCollectorTest;
107109
import org.junit.validator.PublicClassValidatorTest;
108110

109111
// These test files need to be cleaned. See
@@ -204,6 +206,8 @@
204206
JUnitCommandLineParseResultTest.class,
205207
FilterFactoriesTest.class,
206208
CategoryFilterFactoryTest.class,
209+
EventCollectorMatchersTest.class,
210+
EventCollectorTest.class,
207211
FrameworkFieldTest.class,
208212
FrameworkMethodTest.class,
209213
FailOnTimeoutTest.class,

src/test/java/org/junit/testsupport/EventCollector.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ public class EventCollector extends RunListener {
3232
private final List<Description> testsIgnored = synchronizedList(new ArrayList<Description>());
3333

3434
@Override
35-
public void testRunStarted(Description description) throws Exception {
35+
public void testRunStarted(Description description) {
3636
testRunsStarted.add(description);
3737
}
3838

3939
@Override
40-
public void testRunFinished(Result result) throws Exception {
40+
public void testRunFinished(Result result) {
4141
testRunsFinished.add(result);
4242
}
4343

4444
@Override
45-
public void testStarted(Description description) throws Exception {
45+
public void testStarted(Description description) {
4646
testsStarted.add(description);
4747
}
4848

4949
@Override
50-
public void testFinished(Description description) throws Exception {
50+
public void testFinished(Description description) {
5151
testsFinished.add(description);
5252
}
5353

5454
@Override
55-
public void testFailure(Failure failure) throws Exception {
55+
public void testFailure(Failure failure) {
5656
failures.add(failure);
5757
}
5858

@@ -62,7 +62,7 @@ public void testAssumptionFailure(Failure failure) {
6262
}
6363

6464
@Override
65-
public void testIgnored(Description description) throws Exception {
65+
public void testIgnored(Description description) {
6666
testsIgnored.add(description);
6767
}
6868

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,127 @@
1+
package org.junit.testsupport;
2+
3+
import static java.util.Arrays.asList;
4+
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.junit.rules.ExpectedException.none;
8+
9+
import java.util.List;
10+
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.ExpectedException;
14+
import org.junit.runner.Description;
15+
import org.junit.runner.Result;
16+
import org.junit.runner.notification.Failure;
17+
18+
public class EventCollectorTest {
19+
private static final Description DUMMY_DESCRIPTION = Description.EMPTY;
20+
21+
private static final Failure DUMMY_FAILURE = new Failure(null, null);
22+
23+
private static final Result DUMMY_RESULT = new Result();
24+
25+
@Rule
26+
public final ExpectedException thrown = none();
27+
28+
private final EventCollector collector = new EventCollector();
29+
30+
@Test
31+
public void collectsTestRunsStarted() throws Exception {
32+
collector.testRunStarted(DUMMY_DESCRIPTION);
33+
assertThat(collector.getTestRunsStarted(),
34+
is(equalTo(asList(DUMMY_DESCRIPTION))));
35+
}
36+
37+
@Test
38+
public void returnsUnmodifiableListOfTestRunsStarted() throws Exception {
39+
assertNoDescriptionCanBeAddedToList(collector.getTestRunsStarted());
40+
}
41+
42+
@Test
43+
public void collectsTestRunsFinished() throws Exception {
44+
collector.testRunFinished(DUMMY_RESULT);
45+
assertThat(collector.getTestRunsFinished(),
46+
is(equalTo(asList(DUMMY_RESULT))));
47+
}
48+
49+
@Test
50+
public void returnsUnmodifiableListOfTestRunsFinished() throws Exception {
51+
assertNoResultCanBeAddedToList(collector.getTestRunsFinished());
52+
}
53+
54+
@Test
55+
public void collectsTestsStarted() throws Exception {
56+
collector.testStarted(DUMMY_DESCRIPTION);
57+
assertThat(collector.getTestsStarted(),
58+
is(equalTo(asList(DUMMY_DESCRIPTION))));
59+
}
60+
61+
@Test
62+
public void returnsUnmodifiableListOfTestsStarted() throws Exception {
63+
assertNoDescriptionCanBeAddedToList(collector.getTestsStarted());
64+
}
65+
66+
@Test
67+
public void collectsTestsFinished() throws Exception {
68+
collector.testFinished(DUMMY_DESCRIPTION);
69+
assertThat(collector.getTestsFinished(),
70+
is(equalTo(asList(DUMMY_DESCRIPTION))));
71+
}
72+
73+
@Test
74+
public void returnsUnmodifiableListOfTestsFinished() throws Exception {
75+
assertNoDescriptionCanBeAddedToList(collector.getTestsFinished());
76+
}
77+
78+
@Test
79+
public void collectsFailures() throws Exception {
80+
collector.testFailure(DUMMY_FAILURE);
81+
assertThat(collector.getFailures(), is(equalTo(asList(DUMMY_FAILURE))));
82+
}
83+
84+
@Test
85+
public void returnsUnmodifiableListOfFailures() throws Exception {
86+
assertNoFailureCanBeAddedToList(collector.getFailures());
87+
}
88+
89+
@Test
90+
public void collectsAssumptionFailures() throws Exception {
91+
collector.testAssumptionFailure(DUMMY_FAILURE);
92+
assertThat(collector.getAssumptionFailures(),
93+
is(equalTo(asList(DUMMY_FAILURE))));
94+
}
95+
96+
@Test
97+
public void returnsUnmodifiableListOfAssumptionFailures() throws Exception {
98+
assertNoFailureCanBeAddedToList(collector.getAssumptionFailures());
99+
}
100+
101+
@Test
102+
public void collectsTestsIgnored() throws Exception {
103+
collector.testIgnored(DUMMY_DESCRIPTION);
104+
assertThat(collector.getTestsIgnored(),
105+
is(equalTo(asList(DUMMY_DESCRIPTION))));
106+
}
107+
108+
@Test
109+
public void returnsUnmodifiableListOfTestsIgnored() throws Exception {
110+
assertNoDescriptionCanBeAddedToList(collector.getTestsIgnored());
111+
}
112+
113+
private void assertNoDescriptionCanBeAddedToList(List<Description> list) {
114+
thrown.expect(Exception.class);
115+
list.add(DUMMY_DESCRIPTION);
116+
}
117+
118+
private void assertNoFailureCanBeAddedToList(List<Failure> list) {
119+
thrown.expect(Exception.class);
120+
list.add(DUMMY_FAILURE);
121+
}
122+
123+
private void assertNoResultCanBeAddedToList(List<Result> list) {
124+
thrown.expect(Exception.class);
125+
list.add(DUMMY_RESULT);
126+
}
127+
}

0 commit comments

Comments
 (0)