JUnit5: Running a single "instance" of parameterized test

Viewed 145

Let's say we have a parameterized test exampleTest in JUnit5:

public class ExampleTestClass {

    @ParameterizedTest(name = "exampleTest[{0}]")
    @MethodSource("StringParams#getStrings")
    void exampleTest(String input) {
        // ...
    }
}

public class StringParams {
    static Stream<String> getStrings() {
        return Stream.of("a", "b", "c");
    }
}

Is there any way to execute only one instantiation of exampleTest in JUnit5? In JUnit4+Maven+Surefire, the following would work:

mvn test -Dtest="ExampleTestClass#exampleTest[a]"

But it doesn't seem to work with JUnit5+Maven+Surefire. Is it because the test names that get into the test selecting process (which tests will be executed and which are going to be omitted) are different from the one specified in @ParameterizedTest(name = "exampleTest[{0}])?

1 Answers

I haven't tried it but the doc of surefire:test#test reads:

<test>

[...] Since 2.7.3, you can execute a limited number of methods in the test by adding #myMethod or #my*ethod. For example, -Dtest=MyTest#myMethod. This is supported for junit 4.x and TestNg.

Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):

(Emphases by me.)

Related