View Javadoc
1   package org.junit.tests.running.classes;
2   
3   import static org.junit.Assert.fail;
4   
5   import java.util.List;
6   
7   import org.junit.AfterClass;
8   import org.junit.BeforeClass;
9   import org.junit.ClassRule;
10  import org.junit.Ignore;
11  import org.junit.Test;
12  import org.junit.experimental.categories.Categories.CategoryFilter;
13  import org.junit.experimental.categories.Category;
14  import org.junit.rules.TestRule;
15  import org.junit.runner.Description;
16  import org.junit.runner.JUnitCore;
17  import org.junit.runner.Request;
18  import org.junit.runner.Result;
19  import org.junit.runner.notification.Failure;
20  import org.junit.runners.model.Statement;
21  
22  /**
23   * Tests verifying that class-level fixtures ({@link BeforeClass} and
24   * {@link AfterClass}) and rules ({@link ClassRule}) are not executed when there
25   * are no test methods to be run in a test class because they have been ignored.
26   * 
27   */
28  public class ClassLevelMethodsWithIgnoredTestsTest {
29      private static final String FAILURE_MESSAGE = "This should not have happened!";
30  
31      public static class BeforeClassWithIgnoredTest {
32          @BeforeClass
33          public static void beforeClass() {
34              fail(FAILURE_MESSAGE);
35          }
36          
37          @Ignore
38          @Test
39          public void test() throws Exception {
40              fail("test() should not run");
41          }
42      }
43  
44      @Test
45      public void beforeClassShouldNotRunWhenAllTestsAreIgnored() {
46          runClassAndVerifyNoFailures(BeforeClassWithIgnoredTest.class,
47                  "BeforeClass should not have been executed because the test method is ignored!");
48      }
49  
50      @Ignore
51      public static class BeforeClassWithIgnoredClass {
52          @BeforeClass
53          public static void beforeClass() {
54              fail(FAILURE_MESSAGE);
55          }
56  
57          @Test
58          public void test() throws Exception {
59              fail("test() should not run");
60          }
61      }
62  
63      @Test
64      public void beforeClassShouldNotRunWhenWholeClassIsIgnored() {
65          runClassAndVerifyNoFailures(
66                  BeforeClassWithIgnoredClass.class,
67                  "BeforeClass should not have been executed because the whole test class is ignored!");
68      }
69  
70      public static class AfterClassWithIgnoredTest {
71          @Ignore
72          @Test
73          public void test() throws Exception {
74              fail("test() should not run");
75          }
76  
77          @AfterClass
78          public static void afterClass() {
79              fail(FAILURE_MESSAGE);
80          }
81      }
82  
83      @Test
84      public void afterClassShouldNotRunWhenAllTestsAreIgnored() {
85          runClassAndVerifyNoFailures(AfterClassWithIgnoredTest.class,
86                  "AfterClass should not have been executed because the test method is ignored!");
87      }
88  
89      public interface FilteredTests {
90      }
91  
92      public static class BeforeClassWithFilteredTest {
93          @BeforeClass
94          public static void setUpClass() {
95              fail(FAILURE_MESSAGE);
96          }
97  
98          @Category(FilteredTests.class)
99          @Test
100         public void test() throws Exception {
101             fail("test() should not run");
102         }
103     }
104 
105     public static class HasUnfilteredTest {
106         @Test
107         public void unfilteredTest() {
108             // to prevent errors when all other tests have been filtered
109         }
110     }
111 
112     @Test
113     public void beforeClassShouldNotRunWhenAllTestsAreFiltered() {
114         Result result = new JUnitCore().run(Request.classes(
115                 BeforeClassWithFilteredTest.class, HasUnfilteredTest.class)
116                 .filterWith(CategoryFilter.exclude(FilteredTests.class)));
117         analyseResult(
118                 result,
119                 "BeforeClass should not have been executed because the test method is filtered!");
120     }
121 
122     public static class BrokenRule implements TestRule {
123         public Statement apply(Statement base, Description description) {
124             throw new RuntimeException("this rule is broken");
125         }
126     }
127 
128     public static class ClassRuleWithIgnoredTest {
129         @ClassRule
130         public static BrokenRule brokenRule = new BrokenRule();
131 
132         @Ignore
133         @Test
134         public void test() throws Exception {
135             fail("test() should not be run");
136         }
137     }
138 
139     @Test
140     public void classRuleShouldNotBeAppliedWhenAllTestsAreIgnored() {
141         runClassAndVerifyNoFailures(ClassRuleWithIgnoredTest.class,
142                 "The class rule should have been applied because the test method is ignored!");
143     }
144 
145     private void runClassAndVerifyNoFailures(Class<?> klass,
146             String testFailureDescription) {
147         Result result = JUnitCore.runClasses(klass);
148         analyseResult(result, testFailureDescription);
149     }
150 
151     private void analyseResult(Result result, String testFailureDescription) {
152         List<Failure> failures = result.getFailures();
153         if (failures.isEmpty() == false) {
154             analyzeFailure(failures.get(0), testFailureDescription);
155         }
156     }
157 
158     private void analyzeFailure(Failure failure, String testFailureDescription) {
159         String actualFailureMsg = failure.getMessage();
160         if (FAILURE_MESSAGE.equals(actualFailureMsg)) {
161             fail(testFailureDescription);
162         }
163         fail("Unexpected failure : " + actualFailureMsg);
164     }
165 }