Skip to content

Commit 83c56ec

Browse files
committed
Call TestRule @rules for outer classes.
1 parent fbc8a3d commit 83c56ec

File tree

5 files changed

+120
-4
lines changed

5 files changed

+120
-4
lines changed

src/main/java/net/avh4/test/junit/InnerSpecMethodRunner.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import org.junit.After;
44
import org.junit.Before;
5+
import org.junit.Rule;
56
import org.junit.internal.runners.statements.RunAfters;
67
import org.junit.internal.runners.statements.RunBefores;
8+
import org.junit.rules.RunRules;
9+
import org.junit.rules.TestRule;
710
import org.junit.runner.notification.RunNotifier;
811
import org.junit.runners.BlockJUnit4ClassRunner;
912
import org.junit.runners.model.FrameworkMethod;
@@ -61,10 +64,23 @@ protected Statement methodBlock(FrameworkMethod method) {
6164
}
6265
statement = withOuterBefores(statement, outerInstance);
6366
statement = withOuterAfters(statement, outerInstance);
67+
statement = withOuterRules(statement, outerInstance);
6468

6569
return statement;
6670
}
6771

72+
private Statement withOuterRules(Statement statement,
73+
Object outerInstance) {
74+
List<TestRule> fieldRules = new TestClass(outerInstance.getClass())
75+
.getAnnotatedFieldValues(outerInstance, Rule.class,
76+
TestRule.class);
77+
if (!fieldRules.isEmpty()) {
78+
statement = new RunRules(statement, fieldRules, null);
79+
// TODO find an example where the Rule needs a Description
80+
}
81+
return statement;
82+
}
83+
6884
private Statement withOuterAfters(Statement statement,
6985
Object outerInstance) {
7086
final List<FrameworkMethod> outerAfters =

src/test/java/net/avh4/test/junit/InnerSpecMethodRunnerTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import net.avh4.test.junit.test.support.FailingTestExample;
44
import net.avh4.test.junit.test.support.PassingTestExample;
5+
import org.hamcrest.Matchers;
56
import org.junit.Before;
67
import org.junit.Test;
78
import org.junit.runner.Runner;
89

10+
import static org.hamcrest.CoreMatchers.is;
11+
import static org.junit.Assert.assertThat;
912
import static org.junit.Assert.assertTrue;
1013

1114
public class InnerSpecMethodRunnerTest extends RunnerTestBase {
@@ -76,4 +79,32 @@ public void shouldCallOuterAfterMethodsAfterInnerAfterMethods()
7679
assertTrue(
7780
PassingTestExample.valueOf_innerAfterWasCalled_whenOuterAfterWasCalled);
7881
}
82+
83+
@Test
84+
public void shouldCallInnerRules() throws Exception {
85+
PassingTestExample.innerRuleCounter.reset();
86+
runner.run(notifier);
87+
88+
assertThat(PassingTestExample.innerRuleCounter.count(),
89+
Matchers.greaterThan(0));
90+
}
91+
92+
@Test
93+
public void shouldCallOuterRules() throws Exception {
94+
PassingTestExample.outerRuleCounter.reset();
95+
runner.run(notifier);
96+
97+
assertThat(PassingTestExample.outerRuleCounter.count(),
98+
Matchers.greaterThan(0));
99+
}
100+
101+
@Test
102+
public void shouldCallOuterRulesAroundInnerRules() throws Exception {
103+
runner.run(notifier);
104+
105+
int change =
106+
PassingTestExample.valueOf_innerRuleCounter_whenOuterRuleEnded -
107+
PassingTestExample.valueOf_innerRuleCounter_whenOuterRuleStarted;
108+
assertThat(change, is(1));
109+
}
79110
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.avh4.test.junit.test.support;
2+
3+
public class Counter {
4+
private int count;
5+
6+
public void reset() {
7+
count = 0;
8+
}
9+
10+
public void increment() {
11+
count++;
12+
}
13+
14+
public int count() {
15+
return count;
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.avh4.test.junit.test.support;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
class IncrementWhenCalled implements TestRule {
8+
private final Counter counter;
9+
10+
IncrementWhenCalled(Counter counter) {
11+
this.counter = counter;
12+
}
13+
14+
@Override
15+
public Statement apply(final Statement base, Description description) {
16+
return new Statement() {
17+
@Override
18+
public void evaluate() throws Throwable {
19+
counter.increment();
20+
base.evaluate();
21+
}
22+
};
23+
}
24+
}

src/test/java/net/avh4/test/junit/test/support/PassingTestExample.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package net.avh4.test.junit.test.support;
22

3-
import org.junit.After;
4-
import org.junit.Before;
5-
import org.junit.Ignore;
6-
import org.junit.Test;
3+
import org.junit.*;
4+
import org.junit.rules.TestRule;
5+
import org.junit.runner.Description;
6+
import org.junit.runners.model.Statement;
77

88
import static org.junit.Assert.assertTrue;
99
import static org.junit.Assert.fail;
@@ -13,6 +13,29 @@ public class PassingTestExample {
1313
public static boolean outerAfterWasCalled;
1414
public static boolean innerAfterWasCalled;
1515
public static boolean valueOf_innerAfterWasCalled_whenOuterAfterWasCalled;
16+
public static Counter innerRuleCounter = new Counter();
17+
public static Counter outerRuleCounter = new Counter();
18+
public static int valueOf_innerRuleCounter_whenOuterRuleStarted;
19+
public static int valueOf_innerRuleCounter_whenOuterRuleEnded;
20+
21+
@Rule
22+
public TestRule outerRule = new IncrementWhenCalled(outerRuleCounter);
23+
@Rule
24+
public TestRule checkInnerRuleState = new TestRule() {
25+
@Override
26+
public Statement apply(final Statement base, Description description) {
27+
return new Statement() {
28+
@Override
29+
public void evaluate() throws Throwable {
30+
valueOf_innerRuleCounter_whenOuterRuleStarted =
31+
innerRuleCounter.count();
32+
base.evaluate();
33+
valueOf_innerRuleCounter_whenOuterRuleEnded =
34+
innerRuleCounter.count();
35+
}
36+
};
37+
}
38+
};
1639

1740
@Before
1841
public void setup() throws Exception {
@@ -31,6 +54,9 @@ public class Inner1 {
3154
private boolean innerSetupWasCalled;
3255
private boolean valueOf_outerSetupWasCalled_whenInnerSetupWasCalled;
3356

57+
@Rule
58+
public TestRule innerRule = new IncrementWhenCalled(innerRuleCounter);
59+
3460
@Before
3561
public void setup() throws Exception {
3662
innerSetupWasCalled = true;
@@ -69,6 +95,7 @@ public void passesUnlessInnerBeforesWereCalledBeforeOuterBefores()
6995
public void ignored() throws Exception {
7096
fail();
7197
}
98+
7299
}
73100

74101
public class Inner2 {
@@ -83,4 +110,5 @@ public void test2_1() throws Exception {
83110
public void test_1() throws Exception {
84111
//pass
85112
}
113+
86114
}

0 commit comments

Comments
 (0)