Chirag Arora
Chirag Arora

Reputation: 936

JUnit parameterized and non-parameterized tests with PowerMockito

So I am trying to run JUnit parameterized tests along with non-parameterized tests in the same test class. But I am running into one error or the other. Has anyone tried this before and were they successful in doing so? I know other runners need to be used with the @PowerMockRunnerDelegate in order to run correctly. So here's what I came up with:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest(Some.class)
@PowerMockIgnore("javax.management.*")
public class TestClass {

    @PowerMockRunnerDelegate(Parameterized.class)
    public static class ParameterizedTests {

    }

    @Test
    public void nonParameterizedTestOne() {

    }

    @Test
    public void nonParameterizedTestTwo() {

    }

}

But I get the error:

Test class should have exactly one public zero-argument constructor

Without powermock, this situation can be easily handled with:

@RunWith(Enclosed.class)
public class TestClass {

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

    }

    @Test
    public void nonParameterizedTestOne() {

    }

    @Test
    public void nonParameterizedTestTwo() {

    }

}

But I would definitely like to use powermock. Any solutions?

Upvotes: 2

Views: 1017

Answers (1)

Fred
Fred

Reputation: 1101

I had the same issue, this worked for me:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest({Some.class})
public class TestClass {

    private static void setUpOnceForAllTests() {
    }

    private static void setUpForEveryTest() {
    }

    public static class SingleTests {

        // Setup once for all single tests
        @BeforeClass
        public static void setUpBeforeClass() {
            setUpOnceForTests();
        }

        // Setup for each and every single test
        @Before
        public void setUp() {
            setUpForEveryTest();
        }

        @Test
        public void nonParameterizedTestOne() {
        }

        @Test
        public void nonParameterizedTestTwo() {
        }
    }

    @PowerMockRunnerDelegate(Parameterized.class)
    public static class ParameterizedTests {
        
        // Setup once for all parameterized test
        @BeforeClass
        public static void setUpBeforeClass() {
            setUpOnceForTests();
        }

        // Setup for each and every parameterized test
        @Before
        public void setUp() {
            setUpForEveryTest();
        }

        @Parameterized.Parameters
        public static Collection<Enum> param() {
            return new ArrayList<>(Arrays.asList(Enum.values()));
        }

        @Parameterized.Parameter
        public int param;

        @Test
        public void parameterizedTestOne() {
        }

        @Test
        public void parameterizedTestTwo() {
        }
    }
}

Upvotes: 1

Related Questions