Class Parameterized
- java.lang.Object
-
- org.junit.runner.Runner
-
- org.junit.runners.ParentRunner<Runner>
-
- org.junit.runners.Suite
-
- org.junit.runners.Parameterized
-
- All Implemented Interfaces:
Describable,Filterable,Orderable,Sortable
public class Parameterized extends Suite
The custom runnerParameterizedimplements parameterized tests. When running a parameterized test class, instances are created for the cross-product of the test methods and the test data elements.For example, to test the
+operator, write:@RunWith(Parameterized.class) public 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); } }Each instance of
AdditionTestwill be constructed using the three-argument constructor and the data values in the@Parametersmethod.In order that you can easily identify the individual tests, you may provide a name for the
@Parametersannotation. This name is allowed to contain placeholders, which are replaced at runtime. The placeholders are- {index}
- the current parameter index
- {0}
- the first parameter value
- {1}
- the second parameter value
- ...
- ...
In the example given above, the
Parameterizedrunner creates names like[2: 3 + 2 = 5]. If you don't use the name parameter, then the current parameter index is used as name.You can also write:
@RunWith(Parameterized.class) public 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 } }); } @Parameter(0) public int firstSummand; @Parameter(1) public int secondSummand; @Parameter(2) public int sum; @Test public void test() { assertEquals(sum, firstSummand + secondSummand); } }Each instance of
AdditionTestwill be constructed with the default constructor and fields annotated by@Parameterwill be initialized with the data values in the@Parametersmethod.The parameters can be provided as an array, too:
@Parameters public static Object[][] data() { return new Object[][] { { 0, 0, 0 }, { 1, 1, 2 }, { 3, 2, 5 }, { 4, 3, 7 } } }; }Tests with single parameter
If your test needs a single parameter only, you don't have to wrap it with an array. Instead you can provide an
Iterableor an array of objects.@Parameters public static Iterable<? extends Object> data() { return Arrays.asList("first test", "second test"); }or
@Parameters public static Object[] data() { return new Object[] { "first test", "second test" }; }Executing code before/after executing tests for specific parameters
If your test needs to perform some preparation or cleanup based on the parameters, this can be done by adding public static methods annotated with
@BeforeParam/@AfterParam. Such methods should either have no parameters or the same parameters as the test.@BeforeParam public static void beforeTestsForParameter(String onlyParameter) { System.out.println("Testing " + onlyParameter); }Create different runners
By default the
Parameterizedrunner creates a slightly modifiedBlockJUnit4ClassRunnerfor each set of parameters. You can build an ownParameterizedrunner that creates another runner for each set of parameters. Therefore you have to build aParametersRunnerFactorythat creates a runner for eachTestWithParameters. (TestWithParametersare bundling the parameters and the test name.) The factory must have a public zero-arg constructor.public class YourRunnerFactory implements ParametersRunnerFactory { public Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError { return YourRunner(test); } }Use the
Parameterized.UseParametersRunnerFactoryto tell theParameterizedrunner that it should use your factory.@RunWith(Parameterized.class) @UseParametersRunnerFactory(YourRunnerFactory.class) public class YourTest { ... }Avoid creating parameters
With
assumptionsyou can dynamically skip tests. Assumptions are also supported by the@Parametersmethod. Creating parameters is stopped when the assumption fails and none of the tests in the test class is executed. JUnit reports asingle assumption failurefor the whole test class in this case.@Parameters public static Iterable<? extends Object> data() { String os = System.getProperty("os.name").toLowerCase() Assume.assumeTrue(os.contains("win")); return Arrays.asList("first test", "second test"); }- Since:
- 4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceParameterized.AfterParamAnnotation forpublic static voidmethods which should be executed after evaluating tests with particular parameters.static interfaceParameterized.BeforeParamAnnotation forpublic static voidmethods which should be executed before evaluating tests with particular parameters.static interfaceParameterized.ParameterAnnotation for fields of the test class which will be initialized by the method annotated byParameters.static interfaceParameterized.ParametersAnnotation for a method which provides parameters to be injected into the test class constructor byParameterized.static interfaceParameterized.UseParametersRunnerFactoryAdd this annotation to your test class if you want to generate a special runner.-
Nested classes/interfaces inherited from class org.junit.runners.Suite
Suite.SuiteClasses
-
-
Constructor Summary
Constructors Constructor Description Parameterized(java.lang.Class<?> klass)Only called reflectively.
-
Method Summary
-
Methods inherited from class org.junit.runners.Suite
describeChild, emptySuite, getChildren, runChild
-
Methods inherited from class org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, collectInitializationErrors, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, isIgnored, order, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses, withInterruptIsolation
-
-