The custom runner 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:
Parameterized testing is implemented in Junit4 through Parameterized runner.
When executing a parameterized test class, the instance's test methods and test data elements are cross-linked together when the test instance is created.
Here is the test method to test the Fibonacci number:
import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FibonacciTest { @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 } }); } 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)); } }
public class Fibonacci { public static int compute(int n) { int result = 0; if (n <= 1) { result = n; } else { result = compute(n - 1) + compute(n - 2); } return result; } }
Each instance of the FibonacciTest class will be created with a two-argument constructor. And these two parameters will be obtained through methods annotated with @Parameters .
In addition to constructor injection, the @Parameters annotation supports property injection
import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FibonacciTest { @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 // first data value (0) is default public /* cannot be private */ int fInput; @Parameter(1) public /* cannot be private */ int fExpected; @Test public void test() { assertEquals(fExpected, Fibonacci.compute(fInput)); } } public class Fibonacci { ... }Note that currently only injection of public properties is supported
single parameter test
If your test only needs a single parameter, you don't need to wrap it in an array. An iterator or array of objects can be provided in this case.
@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" }; }
Identify each test case
To more easily distinguish each test case in a parameterized test, you can provide a name on the @Parameters annotation.
This name can contain placeholders that will be replaced at runtime.
{index}
: the index of the current parameter{0}, {1}, …
: The first parameter, the second parameter, etc., the parameter value. Note: single quotes'
should be escaped into double single quotes''
.
example
import static org.junit.Assert.assertEquals; import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @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 input; private int expected; public FibonacciTest(int input, int expected) { this.input = input; this.expected = expected; } @Test public void test() { assertEquals(expected, Fibonacci.compute(input)); } } public class Fibonacci { ... }
In the above example, the parameterized runner creates names like [3: fib(3)=2]. If you don't specify a name, the index of the current parameter is used by default.
Original: https://github.com/junit-team/junit4/wiki/Parameterized-tests