Run nested JUnit 5 tests with Maven Surefire
Asked Answered
P

3

9

I'm trying to use JUnit 5 in my side project as a trial before migrating my main project. I'd like to use @Nested tests to make my test classes cleaner.
Everything is fine when I ran my test suite as a whole. However, as soon as I try running just a single test, @Nested ones are not executed.

mvn -Dtest=com.mycompany.test.MyTest surefire:test

Is there any way of getting it to run the selected class and all @Nested ones?

Using JUnit 5.1.0, JUnit platform 1.1.0

<build>
<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
      <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>${org.junit.platform.version}</version>
      </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${org.junit.version}</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>
</build>

Test class:

public class MyTest {

  @Test
  public void thisTestExecutes() { }

  @Nested
  public class NestedTests {
    @Test
    public void thisTestDoesnt() { }
  }
}
Part answered 23/3, 2018 at 15:33 Comment(3)
Looks like a bug in our Surefire provider to me. Can you please open an issue on GitHub? github.com/junit-team/junit5/issues/newChelsea
Thanks Marc, just did that: github.com/junit-team/junit5/issues/1343Margaretamargarete
Have you tried to update the maven-surefire-plugin version to 2.22.0 or later? (see my answer #53434163)Goodnight
R
9

To run all nested classes you just need to add an "*" at end of class name. Something like:

mvn -Dtest=com.mycompany.test.MyTest\* surefire:test

or

mvn -Dtest=com.mycompany.test.MyTest* surefire:test
Reconstruct answered 10/6, 2020 at 16:46 Comment(4)
I had to remove that backslash for it to work, maybe because I'm on Windows, but the * did not need escaping for me.Divisor
to run only one nested class, -Dtest:OuterClass$NestedClassDivisor
to run only one test in a nested class , -Dtest:OuterClass$NestedClass#methodNameDivisor
@AlexanderTaylor this is implementation specific, it can change in any java version; but I do not know of a better wayBobbyebobbysocks
C
0

Facing with same issue, then realized my parent test class name and .java file name are different. I changed my test class name to my .java file name with right click > Refactor > Rename (for possible reference issue). Lastly run my test with following command:

mvn -Dtest=com.mycompany.test.MyTest*

By the way I'm using maven-surefire-plugin 2.22.2 version.

Conradconrade answered 3/7, 2020 at 12:0 Comment(0)
T
0

Whole problem is that nested tests are classes compiled the same way as the anonymous classes with a name containing $. The Surefire and Failsafe excludes these by the default pattern

**/*$*

If you use lambda then these exclusions become more and more important. This should work as well:

mvn test -Dexcludes=nonetest
Trimester answered 3/7, 2020 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.