View Javadoc
1   package org.junit.tests.experimental.theories.runner;
2   
3   import static org.hamcrest.CoreMatchers.allOf;
4   import static org.hamcrest.CoreMatchers.is;
5   import static org.junit.Assert.assertEquals;
6   import static org.junit.Assert.assertThat;
7   import static org.junit.experimental.results.PrintableResult.testResult;
8   import static org.junit.experimental.results.ResultMatchers.failureCountIs;
9   import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
10  import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
11  import org.hamcrest.CoreMatchers;
12  import org.junit.Test;
13  import org.junit.experimental.results.PrintableResult;
14  import org.junit.experimental.theories.DataPoint;
15  import org.junit.experimental.theories.DataPoints;
16  import org.junit.experimental.theories.Theories;
17  import org.junit.experimental.theories.Theory;
18  import org.junit.runner.RunWith;
19  import org.junit.runners.model.TestClass;
20  
21  public class UnsuccessfulWithDataPointFields {
22      @RunWith(Theories.class)
23      public static class HasAFailingTheory {
24          @DataPoint
25          public static int ONE = 1;
26  
27          @Theory
28          public void everythingIsZero(int x) {
29              assertThat(x, is(0));
30          }
31      }
32  
33      @Test
34      public void theoryClassMethodsShowUp() throws Exception {
35          assertThat(new Theories(HasAFailingTheory.class).getDescription()
36                  .getChildren().size(), is(1));
37      }
38  
39      @Test
40      public void theoryAnnotationsAreRetained() throws Exception {
41          assertThat(new TestClass(HasAFailingTheory.class).getAnnotatedMethods(
42                  Theory.class).size(), is(1));
43      }
44  
45      @Test
46      public void canRunTheories() throws Exception {
47          assertThat(testResult(HasAFailingTheory.class),
48                  hasSingleFailureContaining("Expected"));
49      }
50  
51      @RunWith(Theories.class)
52      public static class DoesntUseParams {
53          @DataPoint
54          public static int ONE = 1;
55  
56          @Theory
57          public void everythingIsZero(int x, int y) {
58              assertThat(2, is(3));
59          }
60      }
61  
62      @Test
63      public void reportBadParams() throws Exception {
64          assertThat(testResult(DoesntUseParams.class),
65                  hasSingleFailureContaining("everythingIsZero(\"1\" <from ONE>, \"1\" <from ONE>)"));
66      }
67  
68      @RunWith(Theories.class)
69      public static class NullsOK {
70          @DataPoint
71          public static String NULL = null;
72  
73          @DataPoint
74          public static String A = "A";
75  
76          @Theory
77          public void everythingIsA(String a) {
78              assertThat(a, is("A"));
79          }
80      }
81  
82      @Test
83      public void nullsUsedUnlessProhibited() throws Exception {
84          assertThat(testResult(NullsOK.class),
85                  hasSingleFailureContaining("null"));
86      }
87      
88      @RunWith(Theories.class)
89      public static class TheoriesMustBePublic {
90          @DataPoint
91          public static int THREE = 3;
92  
93          @Theory
94          void numbers(int x) {
95  
96          }
97      }
98  
99      @Test
100     public void theoriesMustBePublic() {
101         assertThat(
102                 testResult(TheoriesMustBePublic.class),
103                 hasSingleFailureContaining("public"));
104     }    
105 
106     @RunWith(Theories.class)
107     public static class DataPointFieldsMustBeStatic {
108         @DataPoint
109         public int THREE = 3;
110         
111         @DataPoints
112         public int[] FOURS = new int[] { 4 };
113         
114         @Theory
115         public void numbers(int x) {
116 
117         }
118     }
119 
120     @Test
121     public void dataPointFieldsMustBeStatic() {
122         assertThat(
123                 testResult(DataPointFieldsMustBeStatic.class),
124                 CoreMatchers.<PrintableResult>both(failureCountIs(2))
125                         .and(
126                                 hasFailureContaining("DataPoint field THREE must be static"))
127                         .and(
128                                 hasFailureContaining("DataPoint field FOURS must be static")));
129     }
130     
131     @RunWith(Theories.class)
132     public static class DataPointMethodsMustBeStatic {
133         @DataPoint
134         public int singleDataPointMethod() {
135             return 1;
136         }
137         
138         @DataPoints
139         public int[] dataPointArrayMethod() {
140             return new int[] { 1, 2, 3 };
141         }
142 
143         @Theory
144         public void numbers(int x) {
145             
146         }
147     }
148     
149     @Test
150     public void dataPointMethodsMustBeStatic() {
151         assertThat(
152                 testResult(DataPointMethodsMustBeStatic.class),
153                 CoreMatchers.<PrintableResult>both(failureCountIs(2))
154                 .and(
155                         hasFailureContaining("DataPoint method singleDataPointMethod must be static"))
156                 .and(
157                         hasFailureContaining("DataPoint method dataPointArrayMethod must be static")));
158     }
159 
160     @RunWith(Theories.class)
161     public static class DataPointFieldsMustBePublic {
162         @DataPoint
163         static int THREE = 3;
164         
165         @DataPoints
166         static int[] THREES = new int[] { 3 };
167 
168         @DataPoint
169         protected static int FOUR = 4;
170         
171         @DataPoints
172         protected static int[] FOURS = new int[] { 4 };
173 
174         @DataPoint
175         private static int FIVE = 5;
176         
177         @DataPoints
178         private static int[] FIVES = new int[] { 5 };
179 
180         @Theory
181         public void numbers(int x) {
182         	
183         }
184     }
185 
186     @Test
187     public void dataPointFieldsMustBePublic() {
188         PrintableResult result = testResult(DataPointFieldsMustBePublic.class);        
189         assertEquals(6, result.failureCount());
190 
191         assertThat(result,
192                 allOf(hasFailureContaining("DataPoint field THREE must be public"),
193                       hasFailureContaining("DataPoint field THREES must be public"),
194                       hasFailureContaining("DataPoint field FOUR must be public"),
195                       hasFailureContaining("DataPoint field FOURS must be public"),
196                       hasFailureContaining("DataPoint field FIVE must be public"),
197                       hasFailureContaining("DataPoint field FIVES must be public")));
198     }
199 
200     @RunWith(Theories.class)
201     public static class DataPointMethodsMustBePublic {
202         @DataPoint
203         static int three() {
204             return 3;
205         }
206         
207         @DataPoints
208         static int[] threes() { 
209             return new int[] { 3 };
210         }
211 
212         @DataPoint
213         protected static int four() {
214             return 4;
215         }
216         
217         @DataPoints
218         protected static int[] fours() {
219             return new int[] { 4 };
220         }
221 
222         @DataPoint
223         private static int five() {
224             return 5;
225         }
226         
227         @DataPoints
228         private static int[] fives() {
229             return new int[] { 5 };
230         }
231 
232         @Theory
233         public void numbers(int x) {
234         	
235         }
236     }
237     
238     @Test
239     public void dataPointMethodsMustBePublic() {
240         PrintableResult result = testResult(DataPointMethodsMustBePublic.class);        
241         assertEquals(6, result.failureCount());
242 
243         assertThat(result,
244                 allOf(hasFailureContaining("DataPoint method three must be public"),
245                       hasFailureContaining("DataPoint method threes must be public"),
246                       hasFailureContaining("DataPoint method four must be public"),
247                       hasFailureContaining("DataPoint method fours must be public"),
248                       hasFailureContaining("DataPoint method five must be public"),
249                       hasFailureContaining("DataPoint method fives must be public")));
250     }
251 }