Create multiple parameter sets in one parameterized class (junit)

后端 未结 8 1038
无人共我
无人共我 2021-01-30 13:05

Currently I have to create a parameterized test class for every method that I want to test with several different inputs. Is there a way to add this together in one file?

<
8条回答
  •  忘了有多久
    2021-01-30 13:19

    I'm sure you are not having this problem anymore, but I thought of 3 ways you can do this, each with its pros and cons. With the Parameterized runner, you'll have to use a workaround.

    - Using more parameters with Parameterized

    In case you have to load the parameters externally, you simply add a parameter for the expected results.

    Pros: less coding, and it runs all the tests.

    Cons: new parameters for each different set of tests.

    @RunWith(Parameterized.class)
    public class CalculatorTest extends TestCase {
        private Calculator calculator;
        private int operator1;
        private int operator2;
        private int expectedSum;
        private int expectedSub;
    
        public CalculatorTest(int operator1, int operator2, int expectedSum, int expectedSub) {
            this.operator1 = operator1;
            this.operator2 = operator2;
        }
    
        @Params
        public static Collection setParameters() {
            Collection params = new ArrayList<>();
            // load the external params here
            // this is an example
            params.add(new Object[] {2, 1, 3, 1});
            params.add(new Object[] {5, 2, 7, 3});
    
            return params;
        }
    
        @Before
        public void createCalculator() {
            calculator = new Calculator();
        }
    
        @Test
        public void addShouldAddTwoNumbers() {
            assertEquals(expectedSum, calculator.add(operator1, operator2));
        }
    
        @Test
        public void subtractShouldSubtractTwoNumbers() {
            assertEquals(expectedSub, calculator.subtract(operator1, operator2));
        }
    
        @After
        public void endTest() {
            calculator = null;
            operator1 = null;
            operator2 = null;
            expectedSum = null;
            expectedSub = null;
        }
    }
    

    - Not using Parameterized runner

    This works fine if you set your parameters programatically.

    Pros: You can have as many tests as you want without having to set a huge set of parameters.

    Cons: More coding, and it stops at the first failure (which might not be a con).

    @RunWith(JUnit4.class)
    public class CalculatorTest extends TestCase {
        private Calculator calculator;
    
        @Before
        public void createCalculator() {
            calculator = new Calculator();
        }
    
        @Test
        public void addShouldAddTwoNumbers() {
            int[] operator1 = {1, 3, 5};
            int[] operator2 = {2, 7, 9};
            int[] expectedResults = {3, 10, 14};
    
            for (int i = 0; i < operator1.length; i++) {
                int actualResult = calculator.add(operator1[i], operator2[i]);
                assertEquals(expectedResults[i], actualResult);
            }
        }
    
        @Test
        public void subtractShouldSubtractTwoNumbers() {
            int[] operator1 = {5, 8, 7};
            int[] operator2 = {1, 2, 10};
            int[] expectedResults = {4, 6, -3};
    
            for (int i = 0; i < operator1.length; i++) {
                int actualResult = calculator.subtract(operator1[i], operator2[i]);
                assertEquals(expectedResults[i], actualResult);
            }
        }
    
        @After
        public void endTest() {
            calculator = null;
        }
    }
    

    - Using JUnitParams

    I have no affiliation with Pragmatists, I just found this a few days ago. This framework runs on top of JUnit and handles parameterized tests differently. Parameters are passed directly to the test method, so you can have in the same class different params for different methods.

    Pros: achieves the same results as the solutions above without workarounds.

    Cons: maybe your company doesn't allow you add a new dependency to the project or forces you to use some bizarre coding rule (like using the Parameterized runners exclusively). Let's face it, it happens more than we'd like to.

    Here's a fine example of JUnitParams in action, and you can get the project/check the code on this Github page.

提交回复
热议问题