Class Parameterized
- All Implemented Interfaces:
Describable
,Filterable
,Orderable
,Sortable
Parameterized
implements 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 AdditionTest
will be constructed using the
three-argument constructor and the data values in the
@Parameters
method.
In order that you can easily identify the individual tests, you may provide a
name for the @Parameters
annotation. 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 Parameterized
runner 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 AdditionTest
will be constructed with the default constructor
and fields annotated by @Parameter
will be initialized
with the data values in the @Parameters
method.
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 Iterable
or 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 Parameterized
runner creates a slightly modified
BlockJUnit4ClassRunner
for each set of parameters. You can build an
own Parameterized
runner that creates another runner for each set of
parameters. Therefore you have to build a ParametersRunnerFactory
that creates a runner for each TestWithParameters
. (
TestWithParameters
are 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.UseParametersRunnerFactory
to tell the Parameterized
runner that it should use your factory.
@RunWith(Parameterized.class) @UseParametersRunnerFactory(YourRunnerFactory.class) public class YourTest { ... }
Avoid creating parameters
With assumptions
you can dynamically skip tests.
Assumptions are also supported by the @Parameters
method.
Creating parameters is stopped when the assumption fails and none of the
tests in the test class is executed. JUnit reports a
single assumption failure
for 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 ClassesModifier and TypeClassDescriptionstatic @interface
Annotation forpublic static void
methods which should be executed after evaluating tests with particular parameters.private static class
static @interface
Annotation forpublic static void
methods which should be executed before evaluating tests with particular parameters.static @interface
Annotation for fields of the test class which will be initialized by the method annotated byParameters
.static @interface
Annotation for a method which provides parameters to be injected into the test class constructor byParameterized
.private static class
static @interface
Add 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
ConstructorsModifierConstructorDescriptionParameterized
(Class<?> klass) Only called reflectively.private
Parameterized
(Class<?> klass, Parameterized.RunnersFactory runnersFactory) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
validateBeforeParamAndAfterParamMethods
(Integer parameterCount) private void
validatePublicStaticVoidMethods
(Class<? extends Annotation> annotation, Integer parameterCount, List<Throwable> errors) 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
-
Constructor Details
-
Method Details
-
validateBeforeParamAndAfterParamMethods
private void validateBeforeParamAndAfterParamMethods(Integer parameterCount) throws InvalidTestClassError - Throws:
InvalidTestClassError
-
validatePublicStaticVoidMethods
private void validatePublicStaticVoidMethods(Class<? extends Annotation> annotation, Integer parameterCount, List<Throwable> errors)
-