2

I have a java class in my android app that contains two or more methods that accept a different collection of parameters. How to write a test class containing multi-parameterized unit tests ?

hint: the problem is that when i write the test class with annotation @RunWith(Parameterized.class), it only accepts one parameterized unit test.

1 Answer 1

2

There are a few options for running tests with different sets of parameters.

JUnit Parameterized

You could use different test classes inside another class with the Enclosed runner.

@RunWith(Enclosed.class)
public class TestClass {

    @RunWith(Parameterized.class)
    public static class FirstParameterizedTest {

        @Parameters
        public static Object[][] data() {
            ...
        }

        @Test
        public void someTest() {
            ...
        }
    }

    public static class SecondParameterizedTest {

        @Parameters
        public static Object[][] data() {
            ...
        }

        @Test
        public void anotherTest() {
            ...
        }
    }
}

JUnitParams

JUnitParams has plenty of ways for specifying the parameters. See https://github.com/Pragmatists/JUnitParams/blob/master/src/test/java/junitparams/usage/SamplesOfUsageTest.java

Here is a simple example.

@RunWith(JUnitParamsRunner.class)
public class TestClass {

    @Test
    @Parameters({"AAA,1", "BBB,2"})
    public void paramsInAnnotation(String p1, Integer p2) {
        ...
    }
}

Burst

Burst uses Enums for specifying test parameters. Different test methods can use different Enums.

@RunWith(BurstJUnit4.class)
public class TestClass {
    enum FirstSetOfParameters {
        A(1,2),
        B(2,3)

        final int x1, x2;

        FirstSetOfParameters( int x1, int x2) {
            this.x1=x1;
            this.x2=x2;
        }
    }

    @Test
    public void firstTest( FirstSetOfParameters parameters) {
        ...
    }

    enum SecondSetOfParameters {
        A(1),
        B(2)

        final int x1;

        SecondSetOfParameters(int x1) {
            this.x1=x1;
        }
    }

    @Test
    public void secondTest(SecondSetOfParameters parameters) {
        ...
    }
}

Other libraries

There are some more libraries. You may have a look at Zohhak and junit-dataprovider.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.