View Javadoc
1   package org.junit.tests.experimental.categories;
2   
3   import static org.junit.Assert.assertThat;
4   import static org.junit.experimental.results.PrintableResult.testResult;
5   import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
6   import static org.junit.experimental.results.ResultMatchers.isSuccessful;
7   
8   import java.util.Collection;
9   import java.util.Collections;
10  
11  import org.junit.Assert;
12  import org.junit.Test;
13  import org.junit.experimental.categories.Categories;
14  import org.junit.experimental.categories.Categories.IncludeCategory;
15  import org.junit.experimental.categories.Category;
16  import org.junit.runner.RunWith;
17  import org.junit.runners.Parameterized;
18  import org.junit.runners.Parameterized.Parameters;
19  import org.junit.runners.Suite.SuiteClasses;
20  
21  public class CategoriesAndParameterizedTest {
22      public static class Token {
23  
24      }
25  
26      @RunWith(Parameterized.class)
27      public static class WellBehavedParameterizedTest {
28          public WellBehavedParameterizedTest(String a) {
29          }
30  
31          @Parameters
32          public static Collection<Object[]> getParameters() {
33              return Collections.singletonList(new Object[]{"a"});
34          }
35  
36          @Test
37          public void testSomething() {
38              Assert.assertTrue(true);
39          }
40      }
41  
42      @RunWith(Parameterized.class)
43      public static class ParameterizedTestWithAttemptedMethodCategory {
44          public ParameterizedTestWithAttemptedMethodCategory(String a) {
45          }
46  
47          @Parameters
48          public static Collection<Object[]> getParameters() {
49              return Collections.singletonList(new Object[]{"a"});
50          }
51  
52          @Test
53          @Category(Token.class)
54          public void testSomething() {
55              Assert.assertTrue(true);
56          }
57      }
58  
59      @RunWith(Parameterized.class)
60      @Category(Token.class)
61      public static class ParameterizedTestWithClassCategory {
62          public ParameterizedTestWithClassCategory(String a) {
63          }
64  
65          @Parameters
66          public static Collection<Object[]> getParameters() {
67              return Collections.singletonList(new Object[]{"a"});
68          }
69  
70          @Test
71          public void testSomething() {
72              Assert.assertTrue(true);
73          }
74      }
75  
76      @Category(Token.class)
77      public static class VanillaCategorizedJUnitTest {
78          @Test
79          public void testSomething() {
80              Assert.assertTrue(true);
81          }
82      }
83  
84      @RunWith(Categories.class)
85      @IncludeCategory(Token.class)
86      @SuiteClasses({VanillaCategorizedJUnitTest.class,
87              WellBehavedParameterizedTest.class,
88              ParameterizedTestWithClassCategory.class})
89      public static class ParameterTokenSuiteWellFormed {
90      }
91  
92      @RunWith(Categories.class)
93      @IncludeCategory(Token.class)
94      @SuiteClasses({ParameterizedTestWithAttemptedMethodCategory.class, VanillaCategorizedJUnitTest.class})
95      public static class ParameterTokenSuiteMalformed {
96      }
97  
98      @RunWith(Categories.class)
99      @IncludeCategory(Token.class)
100     @SuiteClasses({VanillaCategorizedJUnitTest.class, ParameterizedTestWithAttemptedMethodCategory.class})
101     public static class ParameterTokenSuiteMalformedAndSwapped {
102     }
103 
104     @Test
105     public void shouldSucceedWithAParameterizedClassSomewhere() {
106         assertThat(testResult(ParameterTokenSuiteWellFormed.class),
107                 isSuccessful());
108     }
109 
110     @Test
111     public void shouldFailWithMethodLevelCategoryAnnotation() {
112         assertThat(
113                 testResult(ParameterTokenSuiteMalformed.class),
114                 hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
115     }
116 
117     @Test
118     public void shouldFailWithMethodLevelCategoryAnnotationSwapped() {
119         assertThat(
120                 testResult(ParameterTokenSuiteMalformedAndSwapped.class),
121                 hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
122     }
123 }