Skip to content

Commit 7e38e39

Browse files
alb-i986marcphilipp
authored andcommitted
RuleChain#around should not allow null args (#1313)
By throwing IllegalArgEx from around(), we allow for better feedback to users, as the stacktrace will point to the exact line where the null rule is declared.
1 parent 4c0c787 commit 7e38e39

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/main/java/org/junit/rules/RuleChain.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ private RuleChain(List<TestRule> rules) {
9090
* Create a new {@code RuleChain}, which encloses the given {@link TestRule} with
9191
* the rules of the current {@code RuleChain}.
9292
*
93-
* @param enclosedRule the rule to enclose.
93+
* @param enclosedRule the rule to enclose; must not be {@code null}.
9494
* @return a new {@code RuleChain}.
95+
* @throws NullPointerException if the argument {@code enclosedRule} is {@code null}
9596
*/
9697
public RuleChain around(TestRule enclosedRule) {
98+
if (enclosedRule == null) {
99+
throw new NullPointerException("The enclosed rule must not be null");
100+
}
97101
List<TestRule> rulesOfNewChain = new ArrayList<TestRule>();
98102
rulesOfNewChain.add(enclosedRule);
99103
rulesOfNewChain.addAll(rulesStartingWithInnerMost);

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

+38
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package org.junit.rules;
22

33
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;
47
import static org.junit.Assert.assertEquals;
58
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.fail;
610
import static org.junit.experimental.results.PrintableResult.testResult;
711
import static org.junit.rules.RuleChain.outerRule;
812

13+
import java.io.PrintWriter;
14+
import java.io.StringWriter;
915
import java.util.ArrayList;
1016
import java.util.List;
1117

1218
import org.junit.Rule;
1319
import org.junit.Test;
20+
import org.junit.internal.Throwables;
1421
import org.junit.runner.Description;
22+
import org.junit.runner.JUnitCore;
23+
import org.junit.runner.Result;
1524

1625
public class RuleChainTest {
1726
private static final List<String> LOG = new ArrayList<String>();
@@ -55,4 +64,33 @@ public void executeRulesInCorrectOrder() throws Exception {
5564
"finished outer rule");
5665
assertEquals(expectedLog, LOG);
5766
}
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("\tat org.junit.rules.RuleChainTest$RuleChainWithNullRules.<init>(RuleChainTest.java:"));
95+
}
5896
}

0 commit comments

Comments
 (0)