• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams;
2 
3 import org.junit.Rule;
4 import org.junit.Test;
5 import org.junit.rules.ErrorCollector;
6 import org.junit.rules.ExpectedException;
7 import org.junit.rules.TemporaryFolder;
8 import org.junit.rules.TestName;
9 import org.junit.rules.TestRule;
10 import org.junit.rules.TestWatcher;
11 import org.junit.rules.Timeout;
12 import org.junit.runner.JUnitCore;
13 import org.junit.runner.Result;
14 import org.junit.runner.RunWith;
15 
16 import static org.assertj.core.api.Assertions.*;
17 
18 @RunWith(JUnitParamsRunner.class)
19 public class RulesTest {
20     @Rule
21     public TemporaryFolder folder = new TemporaryFolder();
22     @Rule
23     public ExpectedException exception = ExpectedException.none();
24     @Rule
25     public ErrorCollector errors = new ErrorCollector();
26     @Rule
27     public TestName testName = new TestName();
28     @Rule
29     public TestWatcher testWatcher = new TestWatcher() {
30     };
31     @Rule
32     public Timeout timeout = new Timeout(0);
33 
34 
35     @Test
36     @Parameters("")
shouldHandleRulesProperly(String n)37     public void shouldHandleRulesProperly(String n) {
38         assertThat(testName.getMethodName()).isEqualTo("shouldHandleRulesProperly");
39     }
40 
41     @Test
shouldProvideHelpfulExceptionMessageWhenRuleIsUsedImproperly()42     public void shouldProvideHelpfulExceptionMessageWhenRuleIsUsedImproperly() {
43         Result result = JUnitCore.runClasses(ProtectedRuleTest.class);
44 
45         assertThat(result.getFailureCount()).isEqualTo(1);
46         assertThat(result.getFailures().get(0).getException())
47                 .hasMessage("The @Rule 'testRule' must be public.");
48     }
49 
50     // TODO(JUnit4.10) - must be static in JUnit 4.10
51     public static class ProtectedRuleTest {
52         @Rule
53         TestRule testRule;
54 
55         @Test
test()56         public void test() {
57 
58         }
59     }
60 
61 }
62