I have a large number of tests as described below:
The expected value (y_position_expected) is hard coded to the jUnit tests. The tests send a value (x_position) to a method which does some statistics and returns the results (y_position_actual). This result is the actual value which is compared to the expected value.

public class PositionNormalizerTest {

    public Normalizers norman ;

    public void beforeFunction() {
        norman = DislocationUtils.getPositionService().getNormalizers() ;


    public void testAmountForNumberString1() {
        String y_position_expected = 100.0d ;
        double x_position = <A DOUBLE GOES HERE> ;
        double y_position_actual = norman.normalizeYPosition(x_position).getAmount() ;
        assertEquals(y_position_expected, y_position_actual, 0.001) ;

The value of x_position comes from the values of a map which is much larger but similar to what is depicted below:

checkpoints = {"alpha":[0.0d, 10.0d,200.0d], "beta":[50.0d, 44.0d,12.0d]}

The keys of this map are strings and the values are lists of doubles. Accordingly, the test has to run for every single element in every single value.

Given the size of the map (checkpoints) and the number of tests, it takes a very long time to create all the tests manually. Therefore, I am looking for a way to make a single jUnit test class with multiple test cases iterate over the values of the map automatically and run the tests. I tried normal loop but as soon as the assertion comes to a conclusion, may it be a fail or a pass the test case ends without continuing the loop. Is there a way to do this? Can I do it using annotation? Thanks.

1 Answers

Community On

Use parameterized test:

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;

public class PositionNormalizerTest {
    public static Collection<Object[]> data() {
        //here you create and return the collection of your values
        return Arrays.asList(new Object[][]{{"alpha", 0.0d, 10.0d, 200.0d}, {"beta", 50.0d, 44.0d,12.0d}});

    public String key; //alpha

    public double d1; //0.0d

    public double d2; //10.0d

    public double d3; //200.0d

    public void test() {
        //here's your test
        //this method will be executed for every element from the data list

You can get more info about parameterized tests here: Parameterized tests