Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run nested JUnit 5 tests with Maven Surefire

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() { }
  }
}
like image 330
Mateusz Wierciński Avatar asked Mar 23 '18 15:03

Mateusz Wierciński


People also ask

How do I run a JUnit 5 test in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

Does Maven run JUnit tests in parallel?

Maven Dependencies In a nutshell, Surefire provides two ways of executing tests in parallel: Multithreading inside a single JVM process. Forking multiple JVM processes.

Which is the Maven plugin for running JUnit tests?

Running a Single Test Class The JUnit Platform Provider supports the test JVM system property supported by the Maven Surefire Plugin. For example, to run only test methods in the org.

How do I run an integration test in Maven?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.


1 Answers

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
like image 107
Douglas Silva Avatar answered Sep 29 '22 10:09

Douglas Silva