1
1
package org .junit .rules ;
2
2
3
3
import static java .util .Arrays .asList ;
4
+ import static org .hamcrest .CoreMatchers .containsString ;
5
+ import static org .hamcrest .CoreMatchers .equalTo ;
6
+ import static org .hamcrest .MatcherAssert .assertThat ;
4
7
import static org .junit .Assert .assertEquals ;
5
8
import static org .junit .Assert .assertTrue ;
9
+ import static org .junit .Assert .fail ;
6
10
import static org .junit .experimental .results .PrintableResult .testResult ;
7
11
import static org .junit .rules .RuleChain .outerRule ;
8
12
13
+ import java .io .PrintWriter ;
14
+ import java .io .StringWriter ;
9
15
import java .util .ArrayList ;
10
16
import java .util .List ;
11
17
12
18
import org .junit .Rule ;
13
19
import org .junit .Test ;
20
+ import org .junit .internal .Throwables ;
14
21
import org .junit .runner .Description ;
22
+ import org .junit .runner .JUnitCore ;
23
+ import org .junit .runner .Result ;
15
24
16
25
public class RuleChainTest {
17
26
private static final List <String > LOG = new ArrayList <String >();
@@ -55,4 +64,33 @@ public void executeRulesInCorrectOrder() throws Exception {
55
64
"finished outer rule" );
56
65
assertEquals (expectedLog , LOG );
57
66
}
67
+
68
+ @ Test
69
+ public void aroundShouldNotAllowNullRules () {
70
+ RuleChain chain = RuleChain .emptyRuleChain ();
71
+ try {
72
+ chain .around (null );
73
+ fail ("around() should not allow null rules" );
74
+ } catch (NullPointerException e ) {
75
+ assertThat (e .getMessage (), equalTo ("The enclosed rule must not be null" ));
76
+ }
77
+ }
78
+
79
+ public static class RuleChainWithNullRules {
80
+ @ Rule
81
+ public final RuleChain chain = outerRule (new LoggingRule ("outer rule" ))
82
+ .around (null );
83
+
84
+ @ Test
85
+ public void example () {}
86
+ }
87
+
88
+ @ Test
89
+ public void whenRuleChainHasNullRuleTheStacktraceShouldPointToIt () {
90
+ Result result = JUnitCore .runClasses (RuleChainWithNullRules .class );
91
+
92
+ assertThat (result .getFailures ().size (), equalTo (1 ));
93
+ String stacktrace = Throwables .getStacktrace (result .getFailures ().get (0 ).getException ());
94
+ assertThat (stacktrace , containsString ("\t at org.junit.rules.RuleChainTest$RuleChainWithNullRules.<init>(RuleChainTest.java:" ));
95
+ }
58
96
}
0 commit comments