lundi 29 février 2016

JUnitParams - executing method 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.

Aucun commentaire:

Enregistrer un commentaire