类 Parameterized
- java.lang.Object
- org.junit.runner.Runner
- org.junit.runners.ParentRunner<Runner>
- org.junit.runners.Suite
- org.junit.runners.Parameterized
- 所有已实现的接口:
Describable,Filterable,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 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
FibonacciTestwill be constructed using the two-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[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
FibonacciTestwill 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 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }; }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" }; }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 ParameterizedRunnerFactory { 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 { ... }- 从以下版本开始:
- 4.0
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.从类继承的嵌套类/接口 org.junit.runners.Suite
Suite.SuiteClasses
构造器概要
构造器 构造器 说明 Parameterized(Class<?> klass)Only called reflectively.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected List<Runner>getChildren()Returns a list of objects that define the children of this Runner.从类继承的方法 org.junit.runners.Suite
describeChild, emptySuite, runChild
从类继承的方法 org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, collectInitializationErrors, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, isIgnored, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses
构造器详细资料
Parameterized
public Parameterized(Class<?> klass) throws Throwable
Only called reflectively. Do not use programmatically.- 抛出:
Throwable
方法详细资料
getChildren
protected List<Runner> getChildren()
从类复制的说明:ParentRunnerReturns a list of objects that define the children of this Runner.- 覆盖:
getChildren在类中Suite