-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathParameterizedTestTest.java
812 lines (666 loc) · 23.6 KB
/
ParameterizedTestTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package org.junit.tests.running.classes;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.testsupport.EventCollectorMatchers.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runners.MethodSorters;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;
import org.junit.runners.model.InitializationError;
import org.junit.runners.parameterized.ParametersRunnerFactory;
import org.junit.runners.parameterized.TestWithParameters;
import org.junit.testsupport.EventCollector;
import org.junit.testsupport.EventCollectorMatchers;
public class ParameterizedTestTest {
@RunWith(Parameterized.class)
public static class AdditionTest {
@Parameters(name = "{index}: {0} + {1} = {2}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 0, 0 }, { 1, 1, 2 },
{ 3, 2, 5 }, { 4, 3, 7 } });
}
private int firstSummand;
private int secondSummand;
private int sum;
public AdditionTest(int firstSummand, int secondSummand, int sum) {
this.firstSummand = firstSummand;
this.secondSummand = secondSummand;
this.sum = sum;
}
@Test
public void test() {
assertEquals(sum, firstSummand + secondSummand);
}
}
@Test
public void countsRuns() {
Result result = JUnitCore.runClasses(AdditionTest.class);
assertEquals(4, result.getRunCount());
}
@Test
public void countBeforeRun() throws Exception {
Runner runner = Request.aClass(AdditionTest.class).getRunner();
assertEquals(4, runner.testCount());
}
@Test
public void plansNamedCorrectly() throws Exception {
Runner runner = Request.aClass(AdditionTest.class).getRunner();
Description description = runner.getDescription();
assertEquals("[2: 3 + 2 = 5]", description.getChildren().get(2)
.getDisplayName());
}
@RunWith(Parameterized.class)
public static class ThreeFailures {
@Parameters(name = "{index}: x={0}")
public static Collection<Integer> data() {
return Arrays.asList(1, 2, 3);
}
@Parameter(0)
public int unused;
@Test
public void testSomething() {
fail();
}
}
@Test
public void countsFailures() throws Exception {
Result result = JUnitCore.runClasses(ThreeFailures.class);
assertEquals(3, result.getFailureCount());
}
@Test
public void failuresNamedCorrectly() {
Result result = JUnitCore.runClasses(ThreeFailures.class);
assertEquals(
"testSomething[0: x=1](" + ThreeFailures.class.getName() + ")",
result.getFailures().get(0).getTestHeader());
}
@RunWith(Parameterized.class)
public static class ParameterizedWithoutSpecialTestname {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{3}, {3}});
}
public ParameterizedWithoutSpecialTestname(Object something) {
}
@Test
public void testSomething() {
}
}
@Test
public void usesIndexAsTestName() {
Runner runner = Request
.aClass(ParameterizedWithoutSpecialTestname.class).getRunner();
Description description = runner.getDescription();
assertEquals("[1]", description.getChildren().get(1).getDisplayName());
}
@RunWith(Parameterized.class)
public static class AdditionTestWithAnnotatedFields {
@Parameters(name = "{index}: {0} + {1} = {2}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 0, 0 }, { 1, 1, 2 },
{ 3, 2, 5 }, { 4, 3, 7 } });
}
@Parameter(0)
public int firstSummand;
@Parameter(1)
public int secondSummand;
@Parameter(2)
public int sum;
@Test
public void test() {
assertEquals(sum, firstSummand + secondSummand);
}
}
@Test
public void providesDataByAnnotatedFields() {
Result result = JUnitCore.runClasses(AdditionTestWithAnnotatedFields.class);
assertEquals(4, result.getRunCount());
assertEquals(0, result.getFailureCount());
}
@RunWith(Parameterized.class)
static public class BadIndexForAnnotatedFieldTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{0}});
}
@Parameter(2)
public int fInput;
public int fExpected;
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void failureOnInitialization() {
Result result = JUnitCore.runClasses(BadIndexForAnnotatedFieldTest.class);
assertEquals(1, result.getFailureCount());
List<Failure> failures = result.getFailures();
assertThat(failures.get(0).getException().getMessage(), allOf(
containsString("Invalid @Parameter value: 2. @Parameter fields counted: 1. Please use an index between 0 and 0."),
containsString("@Parameter(0) is never used.")));
}
@RunWith(Parameterized.class)
static public class BadNumberOfAnnotatedFieldTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{0, 0}});
}
@Parameter(0)
public int fInput;
public int fExpected;
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void numberOfFieldsAndParametersShouldMatch() {
Result result = JUnitCore.runClasses(BadNumberOfAnnotatedFieldTest.class);
assertEquals(1, result.getFailureCount());
List<Failure> failures = result.getFailures();
assertTrue(failures.get(0).getException().getMessage().contains("Wrong number of parameters and @Parameter fields. @Parameter fields counted: 1, available parameters: 2."));
}
private static String fLog;
@RunWith(Parameterized.class)
static public class BeforeAndAfter {
@BeforeClass
public static void before() {
fLog += "before ";
}
@AfterClass
public static void after() {
fLog += "after ";
}
public BeforeAndAfter(int x) {
}
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{3}});
}
@Test
public void aTest() {
}
}
@Test
public void beforeAndAfterClassAreRun() {
fLog = "";
JUnitCore.runClasses(BeforeAndAfter.class);
assertEquals("before after ", fLog);
}
@RunWith(Parameterized.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class BeforeParamAndAfterParam {
@BeforeClass
public static void before() {
fLog += "beforeClass ";
}
@Parameterized.BeforeParam
public static void beforeParam(String x) {
fLog += "before(" + x + ") ";
}
@Parameterized.AfterParam
public static void afterParam() {
fLog += "afterParam ";
}
@AfterClass
public static void after() {
fLog += "afterClass ";
}
private final String x;
public BeforeParamAndAfterParam(String x) {
this.x = x;
}
@Parameters
public static Collection<String> data() {
return Arrays.asList("A", "B");
}
@Test
public void first() {
fLog += "first(" + x + ") ";
}
@Test
public void second() {
fLog += "second(" + x + ") ";
}
}
@Test
public void beforeParamAndAfterParamAreRun() {
fLog = "";
Result result = JUnitCore.runClasses(BeforeParamAndAfterParam.class);
assertEquals(0, result.getFailureCount());
assertEquals("beforeClass before(A) first(A) second(A) afterParam "
+ "before(B) first(B) second(B) afterParam afterClass ", fLog);
}
@RunWith(Parameterized.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class MultipleBeforeParamAndAfterParam {
@Parameterized.BeforeParam
public static void before1() {
fLog += "before1() ";
}
@Parameterized.BeforeParam
public static void before2(String x) {
fLog += "before2(" + x + ") ";
}
@Parameterized.AfterParam
public static void after2() {
fLog += "after2() ";
}
@Parameterized.AfterParam
public static void after1(String x) {
fLog += "after1(" + x + ") ";
}
private final String x;
public MultipleBeforeParamAndAfterParam(String x) {
this.x = x;
}
@Parameters
public static Collection<String> data() {
return Arrays.asList("A", "B");
}
@Test
public void first() {
fLog += "first(" + x + ") ";
}
@Test
public void second() {
fLog += "second(" + x + ") ";
}
}
@Test
public void multipleBeforeParamAndAfterParamAreRun() {
fLog = "";
Result result = JUnitCore.runClasses(MultipleBeforeParamAndAfterParam.class);
assertEquals(0, result.getFailureCount());
assertEquals("before1() before2(A) first(A) second(A) after1(A) after2() "
+ "before1() before2(B) first(B) second(B) after1(B) after2() ", fLog);
}
@RunWith(Parameterized.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class MultipleParametersBeforeParamAndAfterParam {
@Parameterized.BeforeParam
public static void before(String x, int y) {
fLog += "before(" + x + "," + y + ") ";
}
@Parameterized.AfterParam
public static void after(String x, int y) {
fLog += "after(" + x + "," + y + ") ";
}
private final String x;
private final int y;
public MultipleParametersBeforeParamAndAfterParam(String x, int y) {
this.x = x;
this.y = y;
}
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[]{"A", 1}, new Object[]{"B", 2});
}
@Test
public void first() {
fLog += "first(" + x + "," + y + ") ";
}
@Test
public void second() {
fLog += "second(" + x + "," + y + ") ";
}
}
@Test
public void multipleParametersBeforeParamAndAfterParamAreRun() {
fLog = "";
Result result = JUnitCore.runClasses(MultipleParametersBeforeParamAndAfterParam.class);
assertEquals(0, result.getFailureCount());
assertEquals("before(A,1) first(A,1) second(A,1) after(A,1) "
+ "before(B,2) first(B,2) second(B,2) after(B,2) ", fLog);
}
@RunWith(Parameterized.class)
public static class BeforeParamAndAfterParamError {
@Parameterized.BeforeParam
public void beforeParam(String x) {
}
@Parameterized.AfterParam
private static void afterParam() {
}
public BeforeParamAndAfterParamError(String x) {
}
@Parameters
public static Collection<String> data() {
return Arrays.asList("A", "B");
}
@Test
public void test() {
}
}
@Test
public void beforeParamAndAfterParamValidation() {
fLog = "";
Result result = JUnitCore.runClasses(BeforeParamAndAfterParamError.class);
assertEquals(1, result.getFailureCount());
List<Failure> failures = result.getFailures();
assertThat(failures.get(0).getMessage(), containsString("beforeParam() should be static"));
assertThat(failures.get(0).getMessage(), containsString("afterParam() should be public"));
}
@RunWith(Parameterized.class)
public static class BeforeParamAndAfterParamErrorNumberOfParameters {
@Parameterized.BeforeParam
public static void beforeParam(String x, String y) {
}
@Parameterized.AfterParam
public static void afterParam(String x, String y, String z) {
}
public BeforeParamAndAfterParamErrorNumberOfParameters(String x) {
}
@Parameters
public static Collection<String> data() {
return Arrays.asList("A", "B", "C", "D");
}
@Test
public void test() {
}
}
@Test
public void beforeParamAndAfterParamValidationNumberOfParameters() {
fLog = "";
Result result = JUnitCore.runClasses(BeforeParamAndAfterParamErrorNumberOfParameters.class);
assertEquals(1, result.getFailureCount());
List<Failure> failures = result.getFailures();
assertThat(failures.get(0).getMessage(),
containsString("Method beforeParam() should have 0 or 1 parameter(s)"));
assertThat(failures.get(0).getMessage(),
containsString("Method afterParam() should have 0 or 1 parameter(s)"));
}
@RunWith(Parameterized.class)
static public class EmptyTest {
@BeforeClass
public static void before() {
fLog += "before ";
}
@AfterClass
public static void after() {
fLog += "after ";
}
}
@Test
public void validateClassCatchesNoParameters() {
Result result = JUnitCore.runClasses(EmptyTest.class);
assertEquals(1, result.getFailureCount());
}
@RunWith(Parameterized.class)
static public class IncorrectTest {
@Test
public int test() {
return 0;
}
@Parameters
public static Collection<Object[]> data() {
return Collections.singletonList(new Object[]{1});
}
}
@Test
public void failuresAddedForBadTestMethod() throws Exception {
Result result = JUnitCore.runClasses(IncorrectTest.class);
assertEquals(1, result.getFailureCount());
}
@RunWith(Parameterized.class)
static public class ProtectedParametersTest {
@Parameters
protected static Collection<Object[]> data() {
return Collections.emptyList();
}
@Test
public void aTest() {
}
}
@Test
public void meaningfulFailureWhenParametersNotPublic() {
assertTestCreatesSingleFailureWithMessage(ProtectedParametersTest.class,
"No public static parameters method on class "
+ ProtectedParametersTest.class.getName());
}
@RunWith(Parameterized.class)
static public class ParametersNotIterable {
@Parameters
public static String data() {
return "foo";
}
@Test
public void aTest() {
}
}
@Test
public void meaningfulFailureWhenParametersAreNotAnIterable() {
assertThat(
testResult(ParametersNotIterable.class).toString(),
containsString("ParametersNotIterable.data() must return an Iterable of arrays."));
}
@RunWith(Parameterized.class)
static public class PrivateConstructor {
private PrivateConstructor(int x) {
}
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{3}});
}
@Test
public void aTest() {
}
}
@Test(expected = InitializationError.class)
public void exceptionWhenPrivateConstructor() throws Throwable {
new Parameterized(PrivateConstructor.class);
}
@RunWith(Parameterized.class)
public static class AdditionTestWithArray {
@Parameters(name = "{index}: {0} + {1} = {2}")
public static Object[][] data() {
return new Object[][] { { 0, 0, 0 }, { 1, 1, 2 }, { 3, 2, 5 },
{ 4, 3, 7 } };
}
@Parameter(0)
public int firstSummand;
@Parameter(1)
public int secondSummand;
@Parameter(2)
public int sum;
@Test
public void test() {
assertEquals(sum, firstSummand + secondSummand);
}
}
@Test
public void runsEveryTestOfArray() {
Result result= JUnitCore.runClasses(AdditionTestWithArray.class);
assertEquals(4, result.getRunCount());
}
@RunWith(Parameterized.class)
static public class SingleArgumentTestWithArray {
@Parameters
public static Object[] data() {
return new Object[] { "first test", "second test" };
}
public SingleArgumentTestWithArray(Object argument) {
}
@Test
public void aTest() {
}
}
@Test
public void runsForEverySingleArgumentOfArray() {
Result result= JUnitCore.runClasses(SingleArgumentTestWithArray.class);
assertEquals(2, result.getRunCount());
}
@RunWith(Parameterized.class)
static public class SingleArgumentTestWithIterable {
private static final AtomicBoolean dataCalled = new AtomicBoolean(false);
@Parameters
public static Iterable<? extends Object> data() {
if (!dataCalled.compareAndSet(false, true)) {
fail("Should not call @Parameters method more than once");
}
return new OneShotIterable<String>(asList("first test", "second test"));
}
public SingleArgumentTestWithIterable(Object argument) {
}
@Test
public void aTest() {
}
}
private static class OneShotIterable<T> implements Iterable<T> {
private final Iterable<T> delegate;
private final AtomicBoolean iterated = new AtomicBoolean(false);
OneShotIterable(Iterable<T> delegate) {
this.delegate = delegate;
}
public Iterator<T> iterator() {
if (iterated.compareAndSet(false, true)) {
return delegate.iterator();
}
throw new IllegalStateException("Cannot call iterator() more than once");
}
}
@Test
public void runsForEverySingleArgumentOfIterable() {
Result result= JUnitCore
.runClasses(SingleArgumentTestWithIterable.class);
assertEquals(2, result.getRunCount());
}
@RunWith(Parameterized.class)
static public class SingleArgumentTestWithCollection {
@Parameters
public static Iterable<? extends Object> data() {
return Collections.unmodifiableCollection(asList("first test", "second test"));
}
public SingleArgumentTestWithCollection(Object argument) {
}
@Test
public void aTest() {
}
}
@Test
public void runsForEverySingleArgumentOfCollection() {
Result result= JUnitCore
.runClasses(SingleArgumentTestWithCollection.class);
assertEquals(2, result.getRunCount());
}
static public class ExceptionThrowingRunnerFactory implements
ParametersRunnerFactory {
public Runner createRunnerForTestWithParameters(TestWithParameters test)
throws InitializationError {
throw new InitializationError(
"Called ExceptionThrowingRunnerFactory.");
}
}
@RunWith(Parameterized.class)
@UseParametersRunnerFactory(ExceptionThrowingRunnerFactory.class)
static public class TestWithUseParametersRunnerFactoryAnnotation {
@Parameters
public static Iterable<? extends Object> data() {
return asList("single test");
}
public TestWithUseParametersRunnerFactoryAnnotation(Object argument) {
}
@Test
public void aTest() {
}
}
@Test
public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotation() {
assertTestCreatesSingleFailureWithMessage(
TestWithUseParametersRunnerFactoryAnnotation.class,
"Called ExceptionThrowingRunnerFactory.");
}
private void assertTestCreatesSingleFailureWithMessage(Class<?> test, String message) {
Result result = JUnitCore.runClasses(test);
assertEquals(1, result.getFailures().size());
assertEquals(message, result.getFailures().get(0).getMessage());
}
@RunWith(Parameterized.class)
@UseParametersRunnerFactory(ExceptionThrowingRunnerFactory.class)
public static abstract class UseParameterizedFactoryAbstractTest {
@Parameters
public static Iterable<? extends Object> data() {
return asList("single test");
}
}
public static class UseParameterizedFactoryTest extends
UseParameterizedFactoryAbstractTest {
public UseParameterizedFactoryTest(String parameter) {
}
@Test
public void parameterizedTest() {
}
}
@Test
public void usesParametersRunnerFactoryThatWasSpecifiedByAnnotationInSuperClass() {
assertTestCreatesSingleFailureWithMessage(
UseParameterizedFactoryTest.class,
"Called ExceptionThrowingRunnerFactory.");
}
@RunWith(Parameterized.class)
public static class ParameterizedAssumtionViolation {
static boolean condition;
@Parameters
public static Iterable<String> data() {
assumeTrue(condition);
return Collections.singletonList("foobar");
}
public ParameterizedAssumtionViolation(String parameter) {
}
@Test
public void test1() {
}
@Test
public void test2() {
}
}
@Test
public void assumtionViolationInParameters() {
ParameterizedAssumtionViolation.condition = true;
Result successResult = JUnitCore.runClasses(ParameterizedAssumtionViolation.class);
assertTrue(successResult.wasSuccessful());
assertEquals(2, successResult.getRunCount());
ParameterizedAssumtionViolation.condition = false;
EventCollector collector = new EventCollector();
JUnitCore core = new JUnitCore();
core.addListener(collector);
core.run(ParameterizedAssumtionViolation.class);
assertThat(collector, allOf(
hasNoFailure(), hasSingleAssumptionFailure(),
hasNumberOfTestsStarted(0), hasNumberOfTestsFinished(0)));
}
}