JUnit Parameterized Tests – Complete Guide!

Thanks to the JUnit Parameterized Tests feature which helps us to create parameterized tests with ease. The Parameterized test class has to be annotated with the @RunWith(Parameterized.class). The parameterized test class also meets the following requirements:

  • It is annotated with @RunWith(Parameterized.class).
  • It has a single constructor that contains the test data.
  • It has a static method which annotated with @parameters annotation and generates and returns test data. Each test data is used as a parameter for the test method.
  • It needs a test method that is annotated with @test annotation.

Class Under Test: Addition.java

public class Addition {
    public int AddOperation(int a, int b) {
        int result = a + b;
        System.out.println("Addition with: " + a + " + " + b + " = " + result);
        return result;
    }
}

ParameterizedTestWithConstructor.java

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class ParameterizedTestWithConstructor {
    //Private variables
    private int v1;
    private int v2;
    private int summation;

    //Constructor
    public ParametrizedTestWithConstructor(int p1, int p2, int p3) {
        this.v1 = p1;
        this.v2 = p2;
        this.summation = p3;
    }

    //Creating test data
    @Parameterized.Parameters
    public static List<Object[]> data() {
        Object[][] dataValues = new Object[][] { { 4, 3, 7 }, { 7, 8, 15 }, { 2, 9, 11 } };
        return Arrays.asList(dataValues);
    }

    //Test add operation method of class Addition
    @Test
    public void testAddition() {
        Addition add = new Addition();
        assertEquals("Addition Failed!", summation, add.AddOperation(v1, v2));
        System.out.println("Test for " + v1 + " and " + v2 + " has been passed!\n");
    }
}

It is also possible to inject data values directly into fields without needing a constructor using the @Parameter annotation.

ParameterizedTestWithParameterAnnotation.java

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;

@RunWith(Parameterized.class)
public class ParameterizedTestWithParameterAnnotation {
    //Private variables
    //Variables using together with @Parameter must be public
    @Parameterized.Parameter(value = 0)
    public int v1;
    @Parameterized.Parameter(value = 1)
    public int v2;
    @Parameterized.Parameter(value = 2)
    public int summation;

    //Creating test data
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][] { { 4, 3, 7 }, { 7, 8, 15 }, { 2, 9, 11 } };
        return Arrays.asList(data);
    }

    //Test addoperation method of class Addition
    @Test
    public void testAddition() {
        Addition add = new Addition();
        assertEquals("Addition Failed!", summation, add.AddOperation(v1, v2));
        System.out.println("Test for " + v1 + " and " + v2 + " has been passed!\n");
    }
}

Output:

Addition with: 4 + 3 = 7
Test for 4 and 3 has been passed!

Addition with: 7 + 8 = 15
Test for 7 and 8 has been passed!

Addition with: 2 + 9 = 11
Test for 2 and 9 has been passed!

Github Page

https://github.com/swtestacademy/junit/tree/junit-parametrized-tests 

Summary of JUnit Parameterized Tests

  • You learned how to write parameterized tests with a constructor and without a constructor.
  • You practiced with parametrized test examples.

Thanks for reading.
Onur Baskirt

5 thoughts on “JUnit Parameterized Tests – Complete Guide!”

  1. The test condition,
    assertEquals(“Addition Failed!”, v1+v2, add.AddOperation(v1, v2));

    has the exact same behaviour of the function tested.

    If we are testing a function which sums two variables, we should give input values and the expected result. Like ~3, add.AddOperator(2, 1).
    Here the test is exactly performing the same operation with the function. If one fails the other will also and we will not detect a failure. We do not expect a wrong result from v1+v2 but theoretically we should avoid testing functions with a derivative logic of themselves, here logic is the same. I have seen many people used to do that but this is basically not testing the method especially when methods/functions are more complex.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.