public class Parameterized extends Suite
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 a Fibonacci function, write:
@RunWith(Parameterized.class)
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 int fInput;
private int fExpected;
public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
Each instance of FibonacciTest will be constructed using the
two-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
In the example given above, the Parameterized runner creates
names like [1: fib(3)=2]. 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 FibonacciTest {
@Parameters
public static Iterable<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, Fibonacci.compute(fInput));
}
}
Each instance of FibonacciTest 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 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
{ 5, 5 }, { 6, 8 } };
}
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" };
}
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 ParameterizedRunnerFactory {
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 {
...
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
Parameterized.Parameter
Annotation for fields of the test class which will be initialized by the
method annotated by
Parameters. |
static interface |
Parameterized.Parameters
Annotation for a method which provides parameters to be injected into the
test class constructor by
Parameterized. |
static interface |
Parameterized.UseParametersRunnerFactory
Add this annotation to your test class if you want to generate a special
runner.
|
Suite.SuiteClasses| Modifier and Type | Field and Description |
|---|---|
private static ParametersRunnerFactory |
DEFAULT_FACTORY |
private static java.util.List<Runner> |
NO_RUNNERS |
private java.util.List<Runner> |
runners |
| Constructor and Description |
|---|
Parameterized(java.lang.Class<?> klass)
Only called reflectively.
|
| Modifier and Type | Method and Description |
|---|---|
private java.lang.Iterable<java.lang.Object> |
allParameters() |
private java.util.List<Runner> |
createRunnersForParameters(java.lang.Iterable<java.lang.Object> allParameters,
java.lang.String namePattern,
ParametersRunnerFactory runnerFactory) |
private java.util.List<TestWithParameters> |
createTestsForParameters(java.lang.Iterable<java.lang.Object> allParameters,
java.lang.String namePattern) |
private TestWithParameters |
createTestWithNotNormalizedParameters(java.lang.String pattern,
int index,
java.lang.Object parametersOrSingleParameter) |
private static TestWithParameters |
createTestWithParameters(TestClass testClass,
java.lang.String pattern,
int index,
java.lang.Object[] parameters) |
protected java.util.List<Runner> |
getChildren()
Returns a list of objects that define the children of this Runner.
|
private FrameworkMethod |
getParametersMethod() |
private ParametersRunnerFactory |
getParametersRunnerFactory(java.lang.Class<?> klass) |
private java.lang.Exception |
parametersMethodReturnedWrongType() |
describeChild, emptySuite, runChildchildrenInvoker, classBlock, classRules, collectInitializationErrors, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, isIgnored, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClassesprivate static final ParametersRunnerFactory DEFAULT_FACTORY
private static final java.util.List<Runner> NO_RUNNERS
private final java.util.List<Runner> runners
public Parameterized(java.lang.Class<?> klass)
throws java.lang.Throwable
java.lang.Throwableprivate ParametersRunnerFactory getParametersRunnerFactory(java.lang.Class<?> klass) throws java.lang.InstantiationException, java.lang.IllegalAccessException
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionprotected java.util.List<Runner> getChildren()
ParentRunnergetChildren in class Suiteprivate TestWithParameters createTestWithNotNormalizedParameters(java.lang.String pattern, int index, java.lang.Object parametersOrSingleParameter)
private java.lang.Iterable<java.lang.Object> allParameters()
throws java.lang.Throwable
java.lang.Throwableprivate FrameworkMethod getParametersMethod() throws java.lang.Exception
java.lang.Exceptionprivate java.util.List<Runner> createRunnersForParameters(java.lang.Iterable<java.lang.Object> allParameters, java.lang.String namePattern, ParametersRunnerFactory runnerFactory) throws InitializationError, java.lang.Exception
InitializationErrorjava.lang.Exceptionprivate java.util.List<TestWithParameters> createTestsForParameters(java.lang.Iterable<java.lang.Object> allParameters, java.lang.String namePattern) throws java.lang.Exception
java.lang.Exceptionprivate java.lang.Exception parametersMethodReturnedWrongType()
throws java.lang.Exception
java.lang.Exceptionprivate static TestWithParameters createTestWithParameters(TestClass testClass, java.lang.String pattern, int index, java.lang.Object[] parameters)