When using JUnit's @Parameterized, can I have some tests still run only once

centic picture centic · Sep 25, 2015 · Viewed 25.9k times · Source

I use @Parameterized in many cases to run tests on a number of permutations. This works very well and keeps the test-code itself simple and clean.

However sometimes I would like to have some of the test-methods still run only once as they do not make use of the parameters, is there a way with JUnit to mark the test-method as "singleton" or "run-once"?

Note: This does not concern running single tests in Eclipse, I know how to do that :)

Answer

Stefan Birkner picture Stefan Birkner · Jan 28, 2016

You could structure your test with the Enclosed runner.

@RunWith(Enclosed.class)
public class TestClass {

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

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

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

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

    public static class NotParameterizedPart {
        @Test
        public void someTest() {
            ...
        }
    }
}