Updated April 1, 2023
Introduction to JUnit Parameterized Test
JUnit Parameterized test facilitates developers to repeat the testing of code with a different set of input values in an automated method and saves considerable time in completing the test cycle. Developers use parameters to store test data for various test runs and feed the data to the execution framework when the actual test is run.
Using these kinds of tests, developers can direct the parameters to a variety of data sources and do the testing to verify the expected results. As an example, the data from the E-mail source or Message queue source, or ERP application scenario can be pointed to the parameters in the class, and all these scenarios can be tested easily.
What are the components in Parameterized Tests?
Parameter
It is a variable that holds the test data, and it has a label or name which is unique within the code. It can store data under a variety of data types, i.e., numeric, alphanumeric, etc. Parameters are defined during the test design stage, and they can be for exclusive use in a test or shared with across other tests.
Data Set
Data sets are the values passed to testing at any given point in time. These values are passed through parameters in the test runs. These data sets can be fed manually or through a file or interfaced with another system.
How to Create JUnit Parameterized Tests
The following steps are used to create Parameterized tests.
- Create a Class that has to be tested. An example of this class may be to input two numbers and multiply them and arrive at the result.
- Develop a parameterized test class. This test class should be annotated in the very beginning with @RunWith (Parameterized.class). It indicates the runner’s class name and the runner’s type. If nothing is specified, then the Default runner type is BlockJunit4ClasssRunner. Variables along with their data type, exclusive or shared details are defined in this test class. This class enables tests to run with a new instance, and it also invokes JUnit life cycle methods like setting up and tearing down to associate/release resources.
- A constructor should be created to store the test data. In addition, a placeholder should be provided for all the variables defined in the classes.
- A static method should be created to generate test data and return them to the classes. This should cover all the input variables defined in the classes. There are other methods to link the data sources directly from any other application. Annotation @parameters to generate necessary data for running the tests.
- For setting up the resource, the annotation @before should be used, and annotation @Test should be used to create the test. Finally, an assert statement can be used to verify the test results.
- Test Runner class should be created to execute the test. Junitcore and runclasses with test class names as parameters can be used in test cases execution. Finally, the result can be computed and displayed to the user.
JUnit Parameterized Test – Creation of Class
First of all, create a class that needs to be tested with the Parameters concept. Let’s take a simple class; it will take three numbers and compute the multiplied product of these numbers. The three numbers will also take the end value in the test case class and verify that the written code generates the same value computed through the logic built into it.
Class to be tested
package JunitCheck;
public Class Multiplier {
public int product(int x, int y , int z) {
return x*y*z ;
}
}
Parameterized Test Class Creation
@RunWith (Parameterized.class)
public class MultiplierTest {
private int number1;
private int number2;
private int number3;
private int correctresult;
private Multiplier multiplier;
JUnit Parameterized Testing Example
An end to end coding of parameterized testing is given below
package JunitCheck;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import jave.util.Collection;
import org.junit.Before;
import.org.junit.Test;
import.org.runner.Runwith;
import.org.runners.Parameterized;
@RunWith (Parameterized.class)
public class MultiplierTest {
private int number1;
private int number2;
private int number3;
private int correctresult;
private Multiplier multiplier;
public MultiplierTest (int number1, int number2, int number3, int correctresult) {
super();
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
this.correctresult = correctresult;
}
@Before
public void initialize() {
multiplier = new Multiplier();
}
@Parameterized.parameters
public static Collection input() {
return Arrays.aslist(new Object[ ][ ] { {1, 2, 3, 6} , {2, 4, 5, 40} , {6, 6, 6, 216) ,
{7, 3, 4, 84} , {4, 4, 4, 64});
}
@Test
public void testMultiplierTest() {
System.out.println(“Multiplied Product of 3 numbers = : “ + correctResult );
assert.Equals( correctResult, multiplier.product(number1, number2, number3 ));
}
Test Runner class has to be created to run the Parameterized test
Package JunitCheck;
import org.junit.runner.Result;
import org.junit.runner.JUnitCore;
import org.juit.runner.notification.Failure;
public class Test {
public static void main(String[ ] args) {
Result result = JUnitCore.runclasses(MultiplierTest.class);
For (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.oout.println(result.wasSuccessful());
}
}
Explanation of the above code
- The “Public static void main” method declares the first execution line for the JUnit test.
- Junit core run classes facilitate the execution of test cases using its runclasses. Test class name will be used as a parameter. Multiplier.class will be used as a parameter in our example.
- Results will be processed in a loop using the data, and the output will be compared with passed values, and the result will be declared as success or failure based on the verification.
- Either of the results will be published to the user.
Conclusion
JUnit Parameterized tests are easy to comprehend, develop and manage. The same test can be executed with multi various inputs in quick time. Effectively it reduces the total number of test cases that the developer may have to create, and it brings down the manual effort. This helps developers to organize the tests/results and avoids duplicate testing. The main advantage of this method is that it facilitates Devops to be implemented easily in an organization due to its agility in testing the whole code set quickly and enabling continuous integration of codes in the production environment.
Recommended Articles
This is a guide to JUnit Parameterized Test. Here we discuss What are the components in Parameterized Tests along with How to Create Parameterized Tests. You may also have a look at the following articles to learn more –