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 ;
    @Before
    public void beforeFunction() {
        norman = DislocationUtils.getPositionService().getNormalizers() ;
    }
    @Test
    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.
Problem:
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.
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;
@RunWith(Parameterized.class)
public class PositionNormalizerTest {
    @Parameters
    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}});
    }
    @Parameter 
    public String key; //alpha
    @Parameter(1)
    public double d1; //0.0d
    @Parameter(2)
    public double d2; //10.0d
    @Parameter(3)
    public double d3; //200.0d
    @Test
    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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With