View Javadoc
1   package org.junit.tests.experimental.categories;
2   
3   import org.junit.Test;
4   import org.junit.experimental.categories.Categories;
5   import org.junit.experimental.categories.Category;
6   import org.junit.runner.JUnitCore;
7   import org.junit.runner.Result;
8   import org.junit.runner.RunWith;
9   import org.junit.runners.Suite;
10  
11  import static org.hamcrest.core.Is.is;
12  import static org.junit.Assert.assertThat;
13  import static org.junit.Assert.assertTrue;
14  import static org.junit.Assert.fail;
15  
16  /**
17   * @author tibor17
18   * @version 4.12
19   * @since 4.12
20   */
21  public class JavadocTest {
22      public static interface FastTests {}
23      public static interface SlowTests {}
24      public static interface SmokeTests {}
25  
26      public static class A {
27          public void a() {
28              fail();
29          }
30  
31          @Category(SlowTests.class)
32          @Test
33          public void b() {
34          }
35  
36          @Category({FastTests.class, SmokeTests.class})
37          @Test
38          public void c() {
39          }
40      }
41  
42      @Category({SlowTests.class, FastTests.class})
43      public static class B {
44          @Test
45          public void d() {
46          }
47      }
48  
49      @RunWith(Categories.class)
50      @Categories.IncludeCategory(SlowTests.class)
51      @Suite.SuiteClasses({A.class, B.class})
52      public static class SlowTestSuite {
53          // Will run A.b and B.d, but not A.a and A.c
54      }
55  
56      @RunWith(Categories.class)
57      @Categories.IncludeCategory({FastTests.class, SmokeTests.class})
58      @Suite.SuiteClasses({A.class, B.class})
59      public static class FastOrSmokeTestSuite {
60          // Will run A.c and B.d, but not A.b because it is not any of FastTests or SmokeTests
61      }
62  
63      @Test
64      public void slowTests() {
65          Result testResult= JUnitCore.runClasses(SlowTestSuite.class);
66          assertTrue(testResult.wasSuccessful());
67          assertThat("unexpected run count", testResult.getRunCount(), is(2));
68          assertThat("unexpected failure count", testResult.getFailureCount(), is(0));
69      }
70  
71      @Test
72      public void fastSmokeTests() {
73          Result testResult= JUnitCore.runClasses(FastOrSmokeTestSuite.class);
74          assertTrue(testResult.wasSuccessful());
75          assertThat("unexpected run count", testResult.getRunCount(), is(2));
76          assertThat("unexpected failure count", testResult.getFailureCount(), is(0));
77      }
78  }