Skip to content

Commit 347eb80

Browse files
committed
Separate VerifierRuleTest and ErrorCollectorTest
It is a common practice to collect the tests for a class in a test class whose name is equal to the class' name but with a suffix `Test`. Therefore having all tests for `ErrorCollector` in a class named `ErrorCollectorTest` makes it easier for people to find them (principle of least astonishment).
1 parent 1b4ac07 commit 347eb80

File tree

3 files changed

+227
-214
lines changed

3 files changed

+227
-214
lines changed

src/test/java/org/junit/rules/AllRulesTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BlockJUnit4ClassRunnerOverrideTest.class,
1010
ClassRulesTest.class,
1111
DisableOnDebugTest.class,
12+
ErrorCollectorTest.class,
1213
ExpectedExceptionTest.class,
1314
ExternalResourceRuleTest.class,
1415
MethodRulesTest.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package org.junit.rules;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.experimental.results.PrintableResult;
7+
import org.junit.function.ThrowingRunnable;
8+
import org.junit.internal.AssumptionViolatedException;
9+
10+
import java.util.concurrent.Callable;
11+
12+
import static org.hamcrest.CoreMatchers.is;
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertThat;
15+
import static org.junit.experimental.results.PrintableResult.testResult;
16+
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
17+
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureMatching;
18+
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
19+
20+
public class ErrorCollectorTest {
21+
public static class UsesErrorCollector {
22+
@Rule
23+
public ErrorCollector collector = new ErrorCollector();
24+
25+
@Test
26+
public void example() {
27+
collector.addError(new Throwable("message"));
28+
}
29+
}
30+
31+
@Test
32+
public void usedErrorCollectorShouldFail() {
33+
assertThat(testResult(UsesErrorCollector.class), hasFailureContaining("message"));
34+
}
35+
36+
public static class PassesAssumptionViolatedExceptionToErrorCollector {
37+
@Rule
38+
public ErrorCollector collector = new ErrorCollector();
39+
40+
@Test
41+
public void example() {
42+
collector.addError(new AssumptionViolatedException("message"));
43+
}
44+
}
45+
46+
@Test
47+
public void passingAssumptionViolatedExceptionShouldResultInFailure() {
48+
assertThat(testResult(PassesAssumptionViolatedExceptionToErrorCollector.class), hasSingleFailureMatching(
49+
CoreMatchers.<Throwable>instanceOf(AssertionError.class)));
50+
}
51+
52+
public static class UsesErrorCollectorTwice {
53+
@Rule
54+
public ErrorCollector collector = new ErrorCollector();
55+
56+
@Test
57+
public void example() {
58+
collector.addError(new Throwable("first thing went wrong"));
59+
collector.addError(new Throwable("second thing went wrong"));
60+
}
61+
}
62+
63+
@Test
64+
public void usedErrorCollectorTwiceShouldFail() {
65+
PrintableResult testResult = testResult(UsesErrorCollectorTwice.class);
66+
assertThat(testResult, hasFailureContaining("first thing went wrong"));
67+
assertThat(testResult, hasFailureContaining("second thing went wrong"));
68+
}
69+
70+
public static class UsesErrorCollectorCheckThat {
71+
@Rule
72+
public ErrorCollector collector = new ErrorCollector();
73+
74+
@Test
75+
public void example() {
76+
collector.checkThat(3, is(4));
77+
collector.checkThat(5, is(6));
78+
collector.checkThat("reason 1", 7, is(8));
79+
collector.checkThat("reason 2", 9, is(16));
80+
}
81+
}
82+
83+
@Test
84+
public void usedErrorCollectorCheckThatShouldFail() {
85+
PrintableResult testResult = testResult(UsesErrorCollectorCheckThat.class);
86+
assertThat(testResult, hasFailureContaining("was <3>"));
87+
assertThat(testResult, hasFailureContaining("was <5>"));
88+
assertThat(testResult, hasFailureContaining("reason 1"));
89+
assertThat(testResult, hasFailureContaining("was <7>"));
90+
assertThat(testResult, hasFailureContaining("reason 2"));
91+
assertThat(testResult, hasFailureContaining("was <9>"));
92+
}
93+
94+
public static class UsesErrorCollectorCheckSucceeds {
95+
@Rule
96+
public ErrorCollector collector = new ErrorCollector();
97+
98+
@Test
99+
public void example() {
100+
collector.checkSucceeds(new Callable<Object>() {
101+
public Object call() throws Exception {
102+
throw new RuntimeException("first!");
103+
}
104+
});
105+
collector.checkSucceeds(new Callable<Integer>() {
106+
public Integer call() throws Exception {
107+
throw new RuntimeException("second!");
108+
}
109+
});
110+
Integer result = collector.checkSucceeds(new Callable<Integer>() {
111+
public Integer call() throws Exception {
112+
return 1;
113+
}
114+
});
115+
assertEquals(Integer.valueOf(1), result);
116+
}
117+
}
118+
119+
@Test
120+
public void usedErrorCollectorCheckSucceedsShouldFail() {
121+
PrintableResult testResult = testResult(UsesErrorCollectorCheckSucceeds.class);
122+
assertThat(testResult, hasFailureContaining("first!"));
123+
assertThat(testResult, hasFailureContaining("second!"));
124+
}
125+
126+
public static class UsesErrorCollectorCheckSucceedsWithAssumptionViolatedException {
127+
@Rule
128+
public ErrorCollector collector = new ErrorCollector();
129+
130+
@Test
131+
public void example() {
132+
collector.checkSucceeds(new Callable<Object>() {
133+
public Object call() throws Exception {
134+
throw new AssumptionViolatedException("message");
135+
}
136+
});
137+
}
138+
}
139+
140+
@Test
141+
public void usedErrorCollectorCheckSucceedsWithAssumptionViolatedExceptionShouldFail() {
142+
PrintableResult testResult = testResult(UsesErrorCollectorCheckSucceedsWithAssumptionViolatedException.class);
143+
assertThat(testResult, hasSingleFailureMatching(CoreMatchers.<Throwable>instanceOf(AssertionError.class)));
144+
assertThat(testResult, hasFailureContaining("Callable threw AssumptionViolatedException"));
145+
}
146+
147+
public static class UsesErrorCollectorCheckSucceedsPasses {
148+
@Rule
149+
public ErrorCollector collector = new ErrorCollector();
150+
151+
@Test
152+
public void example() {
153+
assertEquals(3, collector.checkSucceeds(new Callable<Object>() {
154+
public Object call() throws Exception {
155+
return 3;
156+
}
157+
}));
158+
}
159+
}
160+
161+
@Test
162+
public void usedErrorCollectorCheckSucceedsShouldPass() {
163+
PrintableResult testResult = testResult(UsesErrorCollectorCheckSucceedsPasses.class);
164+
assertThat(testResult, isSuccessful());
165+
}
166+
167+
public static class UsesErrorCollectorCheckThrowsMatchingClass {
168+
@Rule
169+
public ErrorCollector collector = new ErrorCollector();
170+
171+
@Test
172+
public void example() {
173+
collector.checkThrows(IllegalArgumentException.class, new ThrowingRunnable() {
174+
public void run() throws Throwable {
175+
throw new IllegalArgumentException();
176+
}
177+
});
178+
}
179+
}
180+
181+
@Test
182+
public void usedErrorCollectorCheckThrowsMatchingClassShouldPass() {
183+
PrintableResult testResult = testResult(UsesErrorCollectorCheckThrowsMatchingClass.class);
184+
assertThat(testResult, isSuccessful());
185+
}
186+
187+
public static class UsesErrorCollectorCheckThrowsClassMismatch {
188+
@Rule
189+
public ErrorCollector collector = new ErrorCollector();
190+
191+
@Test
192+
public void example() {
193+
collector.checkThrows(IllegalArgumentException.class, new ThrowingRunnable() {
194+
public void run() throws Throwable {
195+
throw new NullPointerException();
196+
}
197+
});
198+
}
199+
}
200+
201+
@Test
202+
public void usedErrorCollectorCheckThrowsClassMismatchShouldFail() {
203+
PrintableResult testResult = testResult(UsesErrorCollectorCheckThrowsClassMismatch.class);
204+
assertThat(testResult, hasFailureContaining(
205+
"expected:<java.lang.IllegalArgumentException> but was:<java.lang.NullPointerException>"));
206+
}
207+
208+
public static class UsesErrorCollectorCheckThrowsNothingThrown {
209+
@Rule
210+
public ErrorCollector collector = new ErrorCollector();
211+
212+
@Test
213+
public void example() {
214+
collector.checkThrows(IllegalArgumentException.class, new ThrowingRunnable() {
215+
public void run() throws Throwable {
216+
}
217+
});
218+
}
219+
}
220+
221+
@Test
222+
public void usedErrorCollectorCheckThrowsNothingThrownShouldFail() {
223+
PrintableResult testResult = testResult(UsesErrorCollectorCheckThrowsNothingThrown.class);
224+
assertThat(testResult, hasFailureContaining("but nothing was thrown"));
225+
}
226+
}

0 commit comments

Comments
 (0)