TESTING WITH MULTIPLE INPUTS USING JUNIT PARAMETERIZED RUNNER

Runners are responsible for executing a Junit test class.

Different types of runners provided by Junit are listed below:

Runner Description
org.junit.runners.JUnit4 This is the default test runner for Junit 4
org.junit.runners.Parameterized This test runner is used to run a test case with a set of different inputs.
org.junit.runners.Suite A Suite can be considered as a test class containing different test classes, each test class can be considered to be a test case. A test suite runner is used to run a test suite i.e. group multiple test classes and run them together.

We can ask Junit to run a test class using a specific test runner by using the @RunWith annotation as shown below:

@RunWith(value=Parameterized.class)
public class ParameterizedTest {
    ...
}

We can also create our own test runner by extending the class org.junit.runner.Runner.


Parameterized Test Runner

A parameterized test runner is used to run a test case with different set of inputs. Below is an example that demonstrates how parameterized runner can be used.

We will first create a simple Rectangle class as shown below. It has a method to calculate its perimeter. The perimeter of a rectangle can be calculated by the following formula: 2 x length of rectangle + 2 x width of rectangle.

public class Rectangle {
    
    private int length;
    private int width;
    
    /**
     * @param length
     * @param width
     */
    public Rectangle(int length, int width){
        this.length = length;
        this.width = width;
    }
    
    /**
     * Calculate and returns the 
     * perimeter of the rectangle.
     */
    public int calculatePerimeter(){
        return 2 *(length + width);
    }
}

Below is our Junit Parameterized test runner that we will use to test our Rectangle class with a set of different inputs.

@RunWith(value=Parameterized.class)
public class RectangleTest {
    
    private int expectedResult;
    private int length;
    private int width;
    
    @Parameters
    public static Collection<Integer[]> getInputs(){
        Integer [][] arry = new Integer [][] {
                {60, 20, 10},
                {44, 10, 12},
                {138, 45, 24},
                {176, 34, 54}
        };
        return Arrays.asList(arry);
    }
    
    /**
     * @param expectedResult
     * @param length
     * @param width
     */
    public RectangleTest(int expectedResult, int length, int width){
        this.expectedResult = expectedResult;
        this.length = length;
        this.width = width;
    }
    
    @Test
    public void calculatePerimeterTest(){
        Rectangle rect = new Rectangle(length, width);
        int result = rect.calculatePerimeter();
        assertEquals(expectedResult, result);
    }
}

The test class above demands for some explanation. Below are some of the mandatory requirements for using a parameterized test runner that will simplify our understanding.

  • The class must be annotated with @RunWith and Parameterized class must be passed as its argument. It indicates Junit to run this Test using the Parameterized runner.
  • All instance variables used in the test must be declared.
  • A constructor must be defined to instantiate all the variables declared in the class.
  • It must have a method annotated with @Paramaters (getInputs in this case), the method should be public static and return a Collection type. This method is used to define the different set of inputs used in our test. The inputs are declared as arrays and must be in the same sequence as the parameters in the constructor. The sequence in our case is expectedResult, length and width.

RELATED ARTICLES

Grouping Of Tests using JUnit Categories

JUnit provides the developers with an opportunity to group multiple tests into a single category, thus creating the possibility to include or exclude the category during execution of the test case.

View Article

Testing Exception and Timeout with JUnit

Apart from testing positive scenarios, JUnit can also be used to test negative scenarios. It can be used to test scenarios where an exception is expected or a timeout is to be checked.

View Article

Unit Testing with JUnit

JUnit is a popular unit testing framework that helps developers to unit test their source code during the development of a java application. This articles demonstrates a basic JUnit Test Case.

View Article

Introducing JUnit Rule

Junit Rules allows developers to add additional functionalities that can applied to all test methods in a test class. It is similar to the concept of custom test runners but with reduced restrictions.

View Article

Understanding JUnit Test Suite

A JUnit Test Suite allows developers to bundle multiple test classes together into a single unit (called as test suite) and run them all together at once.

View Article