-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathParameterizedTestTest.java
556 lines (456 loc) · 16.5 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
package org.junit.tests.running.classes;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
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.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;
public class ParameterizedTestTest {
@RunWith(Parameterized.class)
static public class FibonacciTest {
@Parameters(name = "{index}: fib({0})={1}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
{3, 2}, {4, 3}, {5, 5}, {6, 8}});
}
private final int fInput;
private final int fExpected;
public FibonacciTest(int input, int expected) {
fInput = input;
fExpected = expected;
}
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void count() {
Result result = JUnitCore.runClasses(FibonacciTest.class);
assertEquals(7, result.getRunCount());
assertEquals(6, result.getFailureCount());
}
@Test
public void failuresNamedCorrectly() {
Result result = JUnitCore.runClasses(FibonacciTest.class);
assertEquals(
"test[1: fib(1)=1](" + FibonacciTest.class.getName() + ")",
result.getFailures().get(0).getTestHeader());
}
@Test
public void countBeforeRun() throws Exception {
Runner runner = Request.aClass(FibonacciTest.class).getRunner();
assertEquals(7, runner.testCount());
}
@Test
public void plansNamedCorrectly() throws Exception {
Runner runner = Request.aClass(FibonacciTest.class).getRunner();
Description description = runner.getDescription();
assertEquals("[0: fib(0)=0]", description.getChildren().get(0)
.getDisplayName());
}
@RunWith(Parameterized.class)
static public class ParameterizedWithSpecialTestname {
@Parameters(name = "{index,number,0000}: param 1: {0} on test#: {index} with expected result: {1} - {index}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
{3, 2}, {4, 3}, {5, 5}, {6, 8}});
}
private final int fInput;
private final int fExpected;
public ParameterizedWithSpecialTestname(int input, int expected) {
fInput = input;
fExpected = expected;
}
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void countWithSpecialTestname() {
Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class);
assertEquals(7, result.getRunCount());
assertEquals(6, result.getFailureCount());
}
@Test
public void failuresNamedCorrectlyWithSpecialTestname() {
Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class);
assertEquals(
"test[0001: param 1: 1 on test#: 1 with expected result: 1 - 1](" + ParameterizedWithSpecialTestname.class.getName() + ")",
result.getFailures().get(0).getTestHeader());
}
@Test
public void countBeforeRunWithSpecialTestname() throws Exception {
Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner();
assertEquals(7, runner.testCount());
}
@Test
public void plansNamedCorrectlyWithSpecialTestname() throws Exception {
Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner();
Description description = runner.getDescription();
assertEquals("[0000: param 1: 0 on test#: 0 with expected result: 0 - 0]", description.getChildren().get(0)
.getDisplayName());
}
@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)
static public class FibonacciWithParameterizedFieldTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1},
{3, 2}, {4, 3}, {5, 5}, {6, 8}});
}
@Parameter(0)
public int fInput;
@Parameter(1)
public int fExpected;
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void countWithParameterizedField() {
Result result = JUnitCore.runClasses(FibonacciWithParameterizedFieldTest.class);
assertEquals(7, result.getRunCount());
assertEquals(6, result.getFailureCount());
}
@Test
public void failuresNamedCorrectlyWithParameterizedField() {
Result result = JUnitCore.runClasses(FibonacciWithParameterizedFieldTest.class);
assertEquals(String
.format("test[1](%s)", FibonacciWithParameterizedFieldTest.class.getName()), result
.getFailures().get(0).getTestHeader());
}
@Test
public void countBeforeRunWithParameterizedField() throws Exception {
Runner runner = Request.aClass(FibonacciWithParameterizedFieldTest.class).getRunner();
assertEquals(7, runner.testCount());
}
@Test
public void plansNamedCorrectlyWithParameterizedField() throws Exception {
Runner runner = Request.aClass(FibonacciWithParameterizedFieldTest.class).getRunner();
Description description = runner.getDescription();
assertEquals("[0]", description.getChildren().get(0).getDisplayName());
}
@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(2, result.getFailureCount());
List<Failure> failures = result.getFailures();
assertEquals("Invalid @Parameter value: 2. @Parameter fields counted: 1. Please use an index between 0 and 0.",
failures.get(0).getException().getMessage());
assertEquals("@Parameter(0) is never used.", failures.get(1).getException().getMessage());
}
@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)
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)
static public class FibonacciTestWithArray {
@Parameters(name= "{index}: fib({0})={1}")
public static Object[][] data() {
return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
{ 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } };
}
private final int fInput;
private final int fExpected;
public FibonacciTestWithArray(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void test() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
return 0;
}
}
@Test
public void runsEveryTestOfArray() {
Result result = JUnitCore.runClasses(FibonacciTestWithArray.class);
assertEquals(7, 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 {
@Parameters
public static Iterable<? extends Object> data() {
return asList("first test", "second test");
}
public SingleArgumentTestWithIterable(Object argument) {
}
@Test
public void aTest() {
}
}
@Test
public void runsForEverySingleArgumentOfIterable() {
Result result= JUnitCore
.runClasses(SingleArgumentTestWithIterable.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.");
}
}