Parameterized junit testing with more than one test
Asked Answered
R

3

6

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;
    }
}
Riverhead answered 15/4, 2016 at 7:21 Comment(1)
You don't need to set mCalculatorModel to null in the tearDown method because JUnit creates a new instance of the class CalculatorModelPresenterTest for each set of parameters.Gene
H
1

Just add another parameter like this:

 return Arrays.asList(new Object[][]{
            {3.0, 4.0, 7.0, -1.0},
            {4.0, 3.0, 7.0, 1.0},
            {8.0, 2.0, 10.0, 6.0},
            {-1.0, 4.0, 3.0, -5.0},
            {3256.0, 4.0, 3260.0, 3252.0}
    });

...

private double mExpectedSubResult;

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

/* THIS TEST WILL NOW PASS */
@Test
public void testSub() throws Exception {
    final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
    assertEquals(actualResult, mExpectedSubResult, 0);
}
Hbomb answered 15/4, 2016 at 7:37 Comment(1)
That worked. Just have to keep adding the parameters to the array. Thanks.Riverhead
G
11

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);
    } 
}
Gene answered 20/4, 2016 at 20:40 Comment(0)
P
3

With JUnit 4 you cannot have multiple @Parameters methods. You need to add a new class for each operation with new parameters.

Photodrama answered 15/4, 2016 at 7:27 Comment(1)
That was what I was thinking, but I didn't want to keep adding classes to do some simple testing.Riverhead
H
1

Just add another parameter like this:

 return Arrays.asList(new Object[][]{
            {3.0, 4.0, 7.0, -1.0},
            {4.0, 3.0, 7.0, 1.0},
            {8.0, 2.0, 10.0, 6.0},
            {-1.0, 4.0, 3.0, -5.0},
            {3256.0, 4.0, 3260.0, 3252.0}
    });

...

private double mExpectedSubResult;

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

/* THIS TEST WILL NOW PASS */
@Test
public void testSub() throws Exception {
    final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
    assertEquals(actualResult, mExpectedSubResult, 0);
}
Hbomb answered 15/4, 2016 at 7:37 Comment(1)
That worked. Just have to keep adding the parameters to the array. Thanks.Riverhead

© 2022 - 2024 — McMap. All rights reserved.