简体   繁体   中英

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;
    }
}

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);
    } 
}

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM