1 package org.junit.tests.running.classes;
2
3 import static java.util.Arrays.asList;
4 import static org.hamcrest.CoreMatchers.containsString;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertThat;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.experimental.results.PrintableResult.testResult;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.junit.runner.Description;
19 import org.junit.runner.JUnitCore;
20 import org.junit.runner.Request;
21 import org.junit.runner.Result;
22 import org.junit.runner.RunWith;
23 import org.junit.runner.Runner;
24 import org.junit.runner.notification.Failure;
25 import org.junit.runners.Parameterized;
26 import org.junit.runners.Parameterized.Parameter;
27 import org.junit.runners.Parameterized.Parameters;
28 import org.junit.runners.Parameterized.UseParametersRunnerFactory;
29 import org.junit.runners.model.InitializationError;
30 import org.junit.runners.parameterized.ParametersRunnerFactory;
31 import org.junit.runners.parameterized.TestWithParameters;
32
33 public class ParameterizedTestTest {
34 @RunWith(Parameterized.class)
35 static public class FibonacciTest {
36 @Parameters(name = "{index}: fib({0})={1}")
37 public static Iterable<Object[]> data() {
38 return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
39 {3, 2}, {4, 3}, {5, 5}, {6, 8}});
40 }
41
42 private final int fInput;
43
44 private final int fExpected;
45
46 public FibonacciTest(int input, int expected) {
47 fInput = input;
48 fExpected = expected;
49 }
50
51 @Test
52 public void test() {
53 assertEquals(fExpected, fib(fInput));
54 }
55
56 private int fib(int x) {
57 return 0;
58 }
59 }
60
61 @Test
62 public void count() {
63 Result result = JUnitCore.runClasses(FibonacciTest.class);
64 assertEquals(7, result.getRunCount());
65 assertEquals(6, result.getFailureCount());
66 }
67
68 @Test
69 public void failuresNamedCorrectly() {
70 Result result = JUnitCore.runClasses(FibonacciTest.class);
71 assertEquals(
72 "test[1: fib(1)=1](" + FibonacciTest.class.getName() + ")",
73 result.getFailures().get(0).getTestHeader());
74 }
75
76 @Test
77 public void countBeforeRun() throws Exception {
78 Runner runner = Request.aClass(FibonacciTest.class).getRunner();
79 assertEquals(7, runner.testCount());
80 }
81
82 @Test
83 public void plansNamedCorrectly() throws Exception {
84 Runner runner = Request.aClass(FibonacciTest.class).getRunner();
85 Description description = runner.getDescription();
86 assertEquals("[0: fib(0)=0]", description.getChildren().get(0)
87 .getDisplayName());
88 }
89
90 @RunWith(Parameterized.class)
91 public static class ParameterizedWithoutSpecialTestname {
92 @Parameters
93 public static Collection<Object[]> data() {
94 return Arrays.asList(new Object[][]{{3}, {3}});
95 }
96
97 public ParameterizedWithoutSpecialTestname(Object something) {
98 }
99
100 @Test
101 public void testSomething() {
102 }
103 }
104
105 @Test
106 public void usesIndexAsTestName() {
107 Runner runner = Request
108 .aClass(ParameterizedWithoutSpecialTestname.class).getRunner();
109 Description description = runner.getDescription();
110 assertEquals("[1]", description.getChildren().get(1).getDisplayName());
111 }
112
113 @RunWith(Parameterized.class)
114 static public class FibonacciWithParameterizedFieldTest {
115 @Parameters
116 public static Collection<Object[]> data() {
117 return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
118 {3, 2}, {4, 3}, {5, 5}, {6, 8}});
119 }
120
121 @Parameter(0)
122 public int fInput;
123
124 @Parameter(1)
125 public int fExpected;
126
127 @Test
128 public void test() {
129 assertEquals(fExpected, fib(fInput));
130 }
131
132 private int fib(int x) {
133 return 0;
134 }
135 }
136
137 @Test
138 public void countWithParameterizedField() {
139 Result result = JUnitCore.runClasses(FibonacciWithParameterizedFieldTest.class);
140 assertEquals(7, result.getRunCount());
141 assertEquals(6, result.getFailureCount());
142 }
143
144 @Test
145 public void failuresNamedCorrectlyWithParameterizedField() {
146 Result result = JUnitCore.runClasses(FibonacciWithParameterizedFieldTest.class);
147 assertEquals(String
148 .format("test[1](%s)", FibonacciWithParameterizedFieldTest.class.getName()), result
149 .getFailures().get(0).getTestHeader());
150 }
151
152 @Test
153 public void countBeforeRunWithParameterizedField() throws Exception {
154 Runner runner = Request.aClass(FibonacciWithParameterizedFieldTest.class).getRunner();
155 assertEquals(7, runner.testCount());
156 }
157
158 @Test
159 public void plansNamedCorrectlyWithParameterizedField() throws Exception {
160 Runner runner = Request.aClass(FibonacciWithParameterizedFieldTest.class).getRunner();
161 Description description = runner.getDescription();
162 assertEquals("[0]", description.getChildren().get(0).getDisplayName());
163 }
164
165 @RunWith(Parameterized.class)
166 static public class BadIndexForAnnotatedFieldTest {
167 @Parameters
168 public static Collection<Object[]> data() {
169 return Arrays.asList(new Object[][]{{0}});
170 }
171
172 @Parameter(2)
173 public int fInput;
174
175 public int fExpected;
176
177 @Test
178 public void test() {
179 assertEquals(fExpected, fib(fInput));
180 }
181
182 private int fib(int x) {
183 return 0;
184 }
185 }
186
187 @Test
188 public void failureOnInitialization() {
189 Result result = JUnitCore.runClasses(BadIndexForAnnotatedFieldTest.class);
190 assertEquals(2, result.getFailureCount());
191 List<Failure> failures = result.getFailures();
192 assertEquals("Invalid @Parameter value: 2. @Parameter fields counted: 1. Please use an index between 0 and 0.",
193 failures.get(0).getException().getMessage());
194 assertEquals("@Parameter(0) is never used.", failures.get(1).getException().getMessage());
195 }
196
197 @RunWith(Parameterized.class)
198 static public class BadNumberOfAnnotatedFieldTest {
199 @Parameters
200 public static Collection<Object[]> data() {
201 return Arrays.asList(new Object[][]{{0, 0}});
202 }
203
204 @Parameter(0)
205 public int fInput;
206
207 public int fExpected;
208
209 @Test
210 public void test() {
211 assertEquals(fExpected, fib(fInput));
212 }
213
214 private int fib(int x) {
215 return 0;
216 }
217 }
218
219 @Test
220 public void numberOfFieldsAndParametersShouldMatch() {
221 Result result = JUnitCore.runClasses(BadNumberOfAnnotatedFieldTest.class);
222 assertEquals(1, result.getFailureCount());
223 List<Failure> failures = result.getFailures();
224 assertTrue(failures.get(0).getException().getMessage().contains("Wrong number of parameters and @Parameter fields. @Parameter fields counted: 1, available parameters: 2."));
225 }
226
227 private static String fLog;
228
229 @RunWith(Parameterized.class)
230 static public class BeforeAndAfter {
231 @BeforeClass
232 public static void before() {
233 fLog += "before ";
234 }
235
236 @AfterClass
237 public static void after() {
238 fLog += "after ";
239 }
240
241 public BeforeAndAfter(int x) {
242
243 }
244
245 @Parameters
246 public static Collection<Object[]> data() {
247 return Arrays.asList(new Object[][]{{3}});
248 }
249
250 @Test
251 public void aTest() {
252 }
253 }
254
255 @Test
256 public void beforeAndAfterClassAreRun() {
257 fLog = "";
258 JUnitCore.runClasses(BeforeAndAfter.class);
259 assertEquals("before after ", fLog);
260 }
261
262 @RunWith(Parameterized.class)
263 static public class EmptyTest {
264 @BeforeClass
265 public static void before() {
266 fLog += "before ";
267 }
268
269 @AfterClass
270 public static void after() {
271 fLog += "after ";
272 }
273 }
274
275 @Test
276 public void validateClassCatchesNoParameters() {
277 Result result = JUnitCore.runClasses(EmptyTest.class);
278 assertEquals(1, result.getFailureCount());
279 }
280
281 @RunWith(Parameterized.class)
282 static public class IncorrectTest {
283 @Test
284 public int test() {
285 return 0;
286 }
287
288 @Parameters
289 public static Collection<Object[]> data() {
290 return Collections.singletonList(new Object[]{1});
291 }
292 }
293
294 @Test
295 public void failuresAddedForBadTestMethod() throws Exception {
296 Result result = JUnitCore.runClasses(IncorrectTest.class);
297 assertEquals(1, result.getFailureCount());
298 }
299
300 @RunWith(Parameterized.class)
301 static public class ProtectedParametersTest {
302 @Parameters
303 protected static Collection<Object[]> data() {
304 return Collections.emptyList();
305 }
306
307 @Test
308 public void aTest() {
309 }
310 }
311
312 @Test
313 public void meaningfulFailureWhenParametersNotPublic() {
314 assertTestCreatesSingleFailureWithMessage(ProtectedParametersTest.class,
315 "No public static parameters method on class "
316 + ProtectedParametersTest.class.getName());
317 }
318
319 @RunWith(Parameterized.class)
320 static public class ParametersNotIterable {
321 @Parameters
322 public static String data() {
323 return "foo";
324 }
325
326 @Test
327 public void aTest() {
328 }
329 }
330
331 @Test
332 public void meaningfulFailureWhenParametersAreNotAnIterable() {
333 assertThat(
334 testResult(ParametersNotIterable.class).toString(),
335 containsString("ParametersNotIterable.data() must return an Iterable of arrays."));
336 }
337
338 @RunWith(Parameterized.class)
339 static public class PrivateConstructor {
340 private PrivateConstructor(int x) {
341
342 }
343
344 @Parameters
345 public static Collection<Object[]> data() {
346 return Arrays.asList(new Object[][]{{3}});
347 }
348
349 @Test
350 public void aTest() {
351 }
352 }
353
354 @Test(expected = InitializationError.class)
355 public void exceptionWhenPrivateConstructor() throws Throwable {
356 new Parameterized(PrivateConstructor.class);
357 }
358
359 @RunWith(Parameterized.class)
360 static public class FibonacciTestWithArray {
361 @Parameters(name= "{index}: fib({0})={1}")
362 public static Object[][] data() {
363 return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
364 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } };
365 }
366
367 private final int fInput;
368
369 private final int fExpected;
370
371 public FibonacciTestWithArray(int input, int expected) {
372 fInput= input;
373 fExpected= expected;
374 }
375
376 @Test
377 public void test() {
378 assertEquals(fExpected, fib(fInput));
379 }
380
381 private int fib(int x) {
382 return 0;
383 }
384 }
385
386 @Test
387 public void runsEveryTestOfArray() {
388 Result result= JUnitCore.runClasses(FibonacciTestWithArray.class);
389 assertEquals(7, result.getRunCount());
390 }
391
392 @RunWith(Parameterized.class)
393 static public class SingleArgumentTestWithArray {
394 @Parameters
395 public static Object[] data() {
396 return new Object[] { "first test", "second test" };
397 }
398
399 public SingleArgumentTestWithArray(Object argument) {
400 }
401
402 @Test
403 public void aTest() {
404 }
405 }
406
407 @Test
408 public void runsForEverySingleArgumentOfArray() {
409 Result result= JUnitCore.runClasses(SingleArgumentTestWithArray.class);
410 assertEquals(2, result.getRunCount());
411 }
412
413 @RunWith(Parameterized.class)
414 static public class SingleArgumentTestWithIterable {
415 @Parameters
416 public static Iterable<? extends Object> data() {
417 return asList("first test", "second test");
418 }
419
420 public SingleArgumentTestWithIterable(Object argument) {
421 }
422
423 @Test
424 public void aTest() {
425 }
426 }
427
428 @Test
429 public void runsForEverySingleArgumentOfIterable() {
430 Result result= JUnitCore
431 .runClasses(SingleArgumentTestWithIterable.class);
432 assertEquals(2, result.getRunCount());
433 }
434
435 static public class ExceptionThrowingRunnerFactory implements
436 ParametersRunnerFactory {
437 public Runner createRunnerForTestWithParameters(TestWithParameters test)
438 throws InitializationError {
439 throw new InitializationError(
440 "Called ExceptionThrowingRunnerFactory.");
441 }
442 }
443
444 @RunWith(Parameterized.class)
445 @UseParametersRunnerFactory(ExceptionThrowingRunnerFactory.class)
446 static public class TestWithUseParametersRunnerFactoryAnnotation {
447 @Parameters
448 public static Iterable<? extends Object> data() {
449 return asList("single test");
450 }
451
452 public TestWithUseParametersRunnerFactoryAnnotation(Object argument) {
453 }
454
455 @Test
456 public void aTest() {
457 }
458 }
459
460 @Test
461 public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotation() {
462 assertTestCreatesSingleFailureWithMessage(
463 TestWithUseParametersRunnerFactoryAnnotation.class,
464 "Called ExceptionThrowingRunnerFactory.");
465 }
466
467 private void assertTestCreatesSingleFailureWithMessage(Class<?> test, String message) {
468 Result result = JUnitCore.runClasses(test);
469 assertEquals(1, result.getFailures().size());
470 assertEquals(message, result.getFailures().get(0).getMessage());
471 }
472
473 @RunWith(Parameterized.class)
474 @UseParametersRunnerFactory(ExceptionThrowingRunnerFactory.class)
475 public static abstract class UseParameterizedFactoryAbstractTest {
476 @Parameters
477 public static Iterable<? extends Object> data() {
478 return asList("single test");
479 }
480 }
481
482 public static class UseParameterizedFactoryTest extends
483 UseParameterizedFactoryAbstractTest {
484
485 public UseParameterizedFactoryTest(String parameter) {
486
487 }
488
489 @Test
490 public void parameterizedTest() {
491 }
492 }
493
494 @Test
495 public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotationInSuperClass() {
496 assertTestCreatesSingleFailureWithMessage(
497 UseParameterizedFactoryTest.class,
498 "Called ExceptionThrowingRunnerFactory.");
499 }
500 }