Search code examples
javaunit-testingjunitjunit4junitparams

JUnitParams - executing separate methods before test


@RunWith(JUnitParamsRunner.class)
public class MySimpleTest {

    private MyRec rec;
    private Matrix matrix;

    @Before
    public void createRecognizerBeforeEveryExecution() {
        rec = new MyRec();
        matrix = MatrixUtils.createMatrixWithValues();
    }

    public static Iterable<Object[]> data() {
        return Arrays.asList(
                new Object[]{"expectedvalue1", "input1"},
                new Object[]{"expectedvalue2", "input2"}
        );
    }

    @Test
    @Parameters(method = "data")
    public void test1(String output, String input) {
        rec.fun1(matrix);
        assertEquals(output, rec.someFunction(input));
    }

    public static Iterable<Object[]> data2() {
        return Arrays.asList(
                new Object[]{"expectedothervalue1", "input1"},
                new Object[]{"expectedothervalue2", "input2"}
        );
    }

    @Test
    @Parameters(method = "data2")
    public void test2(String output, String input) {
        rec.fun1(matrix);
        rec.fun2(matrix);
        assertEquals(output, rec.someFunction(input));
    }

}

I'm trying to find out what is the proper way to make this test. I'd like to use parametrized test, because it's really convenient way.

So as you can see, in every test function I call some function (fun1 and fun2). But I need to call it only once per every test (e.g. before each parametrized test execution).

Is there any way to tell JUnitParams that it should execute other function before executing all of parametrized tests?

I can't use @Before annotation, because as you can see in test1 I'm not using fun2. It think it should be executed by separate function.


Solution

  • I decided to use TestNG to resolve this problem (code just to show my train of thought):

    import org.testng.Assert;
    import org.testng.annotations.*;
    
    import java.lang.reflect.Method;
    
    public class TempTest {
    
        private Integer number;
    
        @BeforeMethod
        public void init(Method m) {
            number = 5;
            switch(m.getName()) {
                case "test2":
                    fun(10);
                    fun2(5);
                    break;
                case "test1":
                    fun(10);
                    break;
            }
        }
    
        public void fun(int value) {
            number += value;
        }
    
        public void fun2(int value) {
            number -= value;
        }
    
        @Test
        public void test1() {
            Assert.assertEquals(new Integer(15), number);
        }
    
        @Test
        public void test2() {
            Assert.assertEquals(new Integer(10), number);
        }
    
        @Test
        public void test3() {
            Assert.assertEquals(new Integer(5), number);
        }
    
    }