Reputation: 91
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() { }
}
}
Upvotes: 9
Views: 3489
Reputation: 91
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
Upvotes: 9
Reputation: 1123
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
Upvotes: 0
Reputation: 51
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.
Upvotes: 0