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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package org.junit.tests.experimental.rules;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import java.util.concurrent.Callable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.junit.rules.ErrorCollector;
import org.junit.rules.Verifier;
public class VerifierRuleTest {
public static class UsesErrorCollector {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test public void example() {
collector.addError(new Throwable("message"));
}
}
@Test public void usedErrorCollectorShouldFail() {
assertThat(testResult(UsesErrorCollector.class), hasFailureContaining("message"));
}
public static class UsesErrorCollectorTwice {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test public void example() {
collector.addError(new Throwable("first thing went wrong"));
collector.addError(new Throwable("second thing went wrong"));
}
}
@Test public void usedErrorCollectorTwiceShouldFail() {
PrintableResult testResult= testResult(UsesErrorCollectorTwice.class);
assertThat(testResult, hasFailureContaining("first thing went wrong"));
assertThat(testResult, hasFailureContaining("second thing went wrong"));
}
public static class UsesErrorCollectorCheckThat {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test public void example() {
collector.checkThat(3, is(4));
collector.checkThat(5, is(6));
collector.checkThat("reason 1", 7, is(8));
collector.checkThat("reason 2", 9, is(16));
}
}
@Test public void usedErrorCollectorCheckThatShouldFail() {
PrintableResult testResult= testResult(UsesErrorCollectorCheckThat.class);
assertThat(testResult, hasFailureContaining("got: <3>"));
assertThat(testResult, hasFailureContaining("got: <5>"));
assertThat(testResult, hasFailureContaining("reason 1"));
assertThat(testResult, hasFailureContaining("got: <7>"));
assertThat(testResult, hasFailureContaining("reason 2"));
assertThat(testResult, hasFailureContaining("got: <9>"));
}
public static class UsesErrorCollectorCheckSucceeds {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test public void example() {
collector.checkSucceeds(new Callable<Object>() {
public Object call() throws Exception {
throw new RuntimeException("first!");
}
});
collector.checkSucceeds(new Callable<Object>() {
public Object call() throws Exception {
throw new RuntimeException("second!");
}
});
}
}
@Test public void usedErrorCollectorCheckSucceedsShouldFail() {
PrintableResult testResult= testResult(UsesErrorCollectorCheckSucceeds.class);
assertThat(testResult, hasFailureContaining("first!"));
assertThat(testResult, hasFailureContaining("second!"));
}
public static class UsesErrorCollectorCheckSucceedsPasses {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test public void example() {
assertEquals(3, collector.checkSucceeds(new Callable<Object>() {
public Object call() throws Exception {
return 3;
}
}));
}
}
@Test public void usedErrorCollectorCheckSucceedsShouldPass() {
PrintableResult testResult= testResult(UsesErrorCollectorCheckSucceedsPasses.class);
assertThat(testResult, isSuccessful());
}
private static String sequence;
public static class UsesVerifier {
@Rule
public Verifier collector= new Verifier() {
@Override
protected void verify() {
sequence+= "verify ";
}
};
@Test public void example() {
sequence+= "test ";
}
}
@Test public void verifierRunsAfterTest() {
sequence = "";
assertThat(testResult(UsesVerifier.class), isSuccessful());
assertEquals("test verify ", sequence);
}
}
|