Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized junit testing with more than one test

Parameterized testing is good to have different data to feed into your test. However, I created a sample calculator that I want to create parameterized tests for. However, I have found that you can only create 1 set of parameterized data for a single test.

I have created parameterized test for adding 2 numbers with the expected result. This data will not work with the subtract as the expected result will be different.

Is it possible to have parameterized data for each test for add, subtract, multiply, and divide?

Many thanks for any suggestions,

@RunWith(Parameterized.class)
public class CalculatorModelPresenterTest {

    private CalculatorModel mCalculatorModel;

    /* Array of tests */
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {3.0, 4.0, 7.0},
                {4.0, 3.0, 7.0},
                {8.0, 2.0, 10.0},
                {-1.0, 4.0, 3.0},
                {3256.0, 4.0, 3260.0}
        });
    }

    private double mNumberOne;
    private double mNumberTwo;
    private double mExpectedResult;

    /* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
    public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult) {
        mNumberOne = numberOne;
        mNumberTwo = numberTwo;
        mExpectedResult = expectedResult;
    }

    /* THIS TEST WILL PASS AS THE TEST DATA IS FOR ADDING */
    @Test
    public void testAdd() throws Exception {
        final double actualResult = mCalculatorModel.add(mNumberOne, mNumberTwo);
        assertEquals(actualResult, mExpectedResult, 0);
    }

    /* HOWEVER, THIS TEST WILL ALWAYS FAIL AS THE TEST DATA IS CUSTOMIZED FOR THE ADD */
    @Test
    public void testSub() throws Exception {
        final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
        assertEquals(actualResult, mExpectedResult, 0);
    }

    @Before
    public void setUp() throws Exception {
        mCalculatorModel = new CalculatorModel();
    }

    @After
    public void tearDown() throws Exception {
        mCalculatorModel = null;
    }
}
like image 330
ant2009 Avatar asked Dec 10 '22 17:12

ant2009


1 Answers

You could write an inner class for each test with each having a different set of parameters and run it with the Enclosed runner.

@RunWith(Enclosed.class)
public class CalculatorModelPresenterTest {

    @RunWith(Parameterized.class)
    public static class Sum {

        @Parameterized.Parameters
        public static Object[][] data() {
            return new Object[][]{
                {3.0, 4.0, 7.0},
                {4.0, 3.0, 7.0},
                {8.0, 2.0, 10.0},
                {-1.0, 4.0, 3.0},
                {3256.0, 4.0, 3260.0}
            };
        }

        @Parameterized.Parameter(0)
        private double mNumberOne;
        @Parameterized.Parameter(1)
        private double mNumberTwo;
        @Parameterized.Parameter(2)
        private double mExpectedResult;

        @Test
        public void testAdd() throws Exception {
            CalculatorModel calculatorModel = new CalculatorModel();
            double actualResult = calculatorModel.add(mNumberOne, mNumberTwo);
            assertEquals(actualResult, mExpectedResult, 0);
        }
    }   

    @RunWith(Parameterized.class)
    public static class Difference {

        @Parameterized.Parameters
        public static Object[][] data() {
            return new Object[][]{
                {3.0, 4.0, -1.0},
                {4.0, 3.0, 1.0},
                {8.0, 2.0, 6.0},
                {-1.0, 4.0, -5.0},
                {3256.0, 4.0, 3252.0}
            };
        }

        @Parameterized.Parameter(0)
        private double mNumberOne;
        @Parameterized.Parameter(1)
        private double mNumberTwo;
        @Parameterized.Parameter(2)
        private double mExpectedResult;

        @Test
        public void testSub() throws Exception {
            CalculatorModel calculatorModel = new CalculatorModel();
            double actualResult = calculatorModel.sub(mNumberOne, mNumberTwo);
            assertEquals(actualResult, mExpectedResult, 0);
        }
    } 
}

An alternative approach would be to use the JUnitParams runner.

@RunWith(JUnitParamsRunner.class)
public class CalculatorModelPresenterTest {

    @Test
    @Parameters({
            "3.0, 4.0, 7.0",
            "4.0, 3.0, 7.0",
            "8.0, 2.0, 10.0",
            "-1.0, 4.0, 3.0",
            "3256.0, 4.0, 3260.0" })
    public void testAdd(double first, double second, double expectedResult)
            throws Exception {
        CalculatorModel calculatorModel = new CalculatorModel();
        double actualResult = calculatorModel.add(first, second);
        assertEquals(actualResult, expectedResult, 0);
    }   

    @Test
    @Parameters({
            "3.0, 4.0, -1.0",
            "4.0, 3.0, 1.0",
            "8.0, 2.0, 6.0",
            "-1.0, 4.0, -5.0",
            "3256.0, 4.0, 3252.0" })
    public void testSub(double first, double second, double expectedResult)
            throws Exception {
        CalculatorModel calculatorModel = new CalculatorModel();
        double actualResult = calculatorModel.sub(first, second);
        assertEquals(actualResult, expectedResult, 0);
    } 
}
like image 155
Stefan Birkner Avatar answered Jan 13 '23 23:01

Stefan Birkner