javaeclipsejunittest-runner

JUnitParamsRunner cannot be resolved


I am trying to test using the JUnitParamsRunner using the Eclipse IDE but somehow the JUnitParamsRunner cannot be resolved . I am currently using JDK 21 and JUnit 5. The @Parameters cannot be resolved to a type and same goes to the @RunWith(JUnitParamsRunner.class)

Here is my JUnit test code

package application;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

import static junitparams.JUnitParamsRunner.$;

import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;

@RunWith(JUnitParamsRunner.class)
public class BasicParameterizedDemo {

    @Test
    public void testAddTwoNumbersV1() {
        VariousMethodsClass vmc = new VariousMethodsClass();
        int result = vmc.addTwoNumbers(10, 15);
        assertEquals(25, result);
    }
    
    @Test
    public void testAddTwoNumbersV2() {
        VariousMethodsClass vmc = new VariousMethodsClass();
        int result = vmc.addTwoNumbers(-50, 10);
        assertEquals(-40, result);
    }
    
    @Test
    public void testAddTwoNumbersV3() {
        VariousMethodsClass vmc = new VariousMethodsClass();
        int result = vmc.addTwoNumbers(30, 100);
        assertEquals(130, result);
    }

    @Test
    @Parameters({"10, 15, 25","-50, 10, -40","30, 100, 130"})
    public void paramTestAddTwoNumbersV1(int num1, int num2, int expectedSum) {
        VariousMethodsClass vmc = new VariousMethodsClass();
        int result = vmc.addTwoNumbers(num1, num2);
        assertEquals(expectedSum, result);
    }   
    
    private Object[] getNumbersToAdd() {
        
        return new Object[] {
            new Object[] {10, 15, 25},
            new Object[] {-50, 10, -40},
            new Object[] {30, 100, 130}
        };
    }
    
    @Test
    @Parameters(method = "getNumbersToAdd")
    public void paramTestAddTwoNumbersV2(int num1, int num2, int expectedSum) {
        VariousMethodsClass vmc = new VariousMethodsClass();
        int result = vmc.addTwoNumbers(num1, num2);
        assertEquals(expectedSum, result);
    }
    
    @Test
    @Parameters
    public void paramTestV3(int num1, int num2, int expectedSum){
        VariousMethodsClass vmc = new VariousMethodsClass();
        assertEquals(expectedSum,vmc.addTwoNumbers(num1, num2));
    }
    
    private Object[] parametersForParamTestV3(){
        return new Object[] {
                new Object[] {10, 15, 25},
                new Object[] {-50, 10, -40},
                new Object[] {30, 100, 130}
        };
    }
    
    @Test
    @Parameters(method = "getNumbersToAdd_V2")
    public void paramTestV4(int num1, int num2, int expectedSum){
        VariousMethodsClass vmc = new VariousMethodsClass();
        assertEquals(expectedSum, vmc.addTwoNumbers(num1, num2));
    }
    
    private Object[] getNumbersToAdd_V2(){
        return $(
                $(10, 15, 25),
                $(-50, 10, -40),
                $(30, 100, 130)
        );
    }
}```

Solution

  • Your imports (org.junit.Test and org.junit.runner.RunWith) are from JUnit 4 (https://github.com/junit-team/junit4/wiki/Getting-started) and JUnit 4 doesn't natively support parameterized tests. For JUnit 4 there is a separate package JUnitParams that you did not import into your project.

    If you want to use JUnit 5 you need to start with importing the correct Test annotation (https://junit.org/junit5/docs/current/user-guide/#writing-tests):

    import org.junit.jupiter.api.Test;
    

    And you need to change your parameterized test to use the native JUnit 5 parameterized test support (https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests):

    import org.junit.jupiter.params.ParameterizedTest
    
        @ParameterizedTest
        @CsvSource({"10, 15, 25","-50, 10, -40","30, 100, 130"})
        public void paramTestAddTwoNumbersV1(int num1, int num2, int expectedSum) {
            VariousMethodsClass vmc = new VariousMethodsClass();
            int result = vmc.addTwoNumbers(num1, num2);
            assertEquals(expectedSum, result);
        }   
     
        private Object[] getNumbersToAdd() {
            return new Object[] {
                new Object[] {10, 15, 25},
                new Object[] {-50, 10, -40},
                new Object[] {30, 100, 130}
            };
        }
    
        @ParameterizedTest
        @MethodSource("getNumbersToAdd")
        public void paramTestAddTwoNumbersV2(int num1, int num2, int expectedSum) {
            VariousMethodsClass vmc = new VariousMethodsClass();
            int result = vmc.addTwoNumbers(num1, num2);
            assertEquals(expectedSum, result);
        }
    
        @ParameterizedTest
        @MethodSource
        public void paramTestV3(int num1, int num2, int expectedSum){
            VariousMethodsClass vmc = new VariousMethodsClass();
            assertEquals(expectedSum,vmc.addTwoNumbers(num1, num2));
        }
    
        private Object[] parametersForParamTestV3(){
            return new Object[] {
                new Object[] {10, 15, 25},
                new Object[] {-50, 10, -40},
                new Object[] {30, 100, 130}
            };
        }
    

    I omitted the last example because JUnit 5 doesn't support the $() for writing arrays.