The class Parameterized is a runner which can be used to run multiple instances of a test class with different argument values passed to its constructor.
For Parameterized runner to work, we need to create a static method annotated with @Parameters in the test class. The return type of this static method should be a Iterable . This Iterable typically contains arrays. Each element of such array should have values assignable to the constructor parameters of the test class.
Let's understand how that works with an example.
Example
Let's create a class which implements some algorithm and is needed to be tested.
public class FactorialCalculator {
public static BigInteger calculateFactorial(BigInteger input) {
BigInteger factorial = BigInteger.ONE;
BigInteger i = BigInteger.ONE;
for (; i.compareTo(input) <= 0; i = i.add(BigInteger.ONE)) {
factorial = factorial.multiply(i);
}
return factorial;
}
}
The Test class
@RunWith(Parameterized.class)
public class FactorialCalcTest {
private final BigInteger inputNumber;
private final BigInteger outputFactorial;
public FactorialCalcTest(BigInteger inputNumber, BigInteger outputFactorial) {
this.inputNumber = inputNumber;
this.outputFactorial = outputFactorial;
}
@Parameterized.Parameters
public static List<BigInteger[]> myParameters() {
BigInteger[][] params = {
param("5", "120"),
param("7", "5040"),
param("10", "3628800"),
param("20", "2432902008176640000")
};
return Arrays.asList(params);
}
private static BigInteger[] param(String input, String output) {
return new BigInteger[]{new BigInteger(input), new BigInteger(output)};
}
@Test
public void testCalculator() {
BigInteger output = FactorialCalculator.calculateFactorial(inputNumber);
System.out.printf("Testing input: %2s, expected: %s, calculated: %s%n",
inputNumber, outputFactorial, output);
Assert.assertEquals(output, outputFactorial);
}
} mvn test -Dtest=FactorialCalcTest Outputd:\example-projects\junit\parameterized-example>mvn test -Dtest=FactorialCalcTest [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building parameterized-example 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ parameterized-example --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit\parameterized-example\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ parameterized-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ parameterized-example --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit\parameterized-example\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ parameterized-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.20:test (default-test) @ parameterized-example --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.FactorialCalcTest Testing input: 5, expected: 120, calculated: 120 Testing input: 7, expected: 5040, calculated: 5040 Testing input: 10, expected: 3628800, calculated: 3628800 Testing input: 20, expected: 2432902008176640000, calculated: 2432902008176640000 [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 s - in com.logicbig.example.FactorialCalcTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.616 s [INFO] Finished at: 2017-07-26T00:42:09-05:00 [INFO] Final Memory: 10M/245M [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
- JDK 1.8
- Maven 3.3.9
|
|