使用 Maven Surefire 运行嵌套的 JUnit 5 测试

Mat*_*ski 7 junit nested maven maven-surefire-plugin junit5

在迁移我的主项目之前,我试图在我的副项目中使用 JUnit 5 作为试验。我想使用 @Nested 测试使我的测试类更干净。
当我整体运行测试套件时,一切都很好。但是,只要我尝试只运行一个测试,就不会执行 @Nested 测试。

mvn -Dtest=com.mycompany.test.MyTest surefire:test
Run Code Online (Sandbox Code Playgroud)

有没有办法让它运行选定的类和所有@Nested 类?

使用 JUnit 5.1.0,JUnit 平台 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>
Run Code Online (Sandbox Code Playgroud)

测试类:

public class MyTest {

  @Test
  public void thisTestExecutes() { }

  @Nested
  public class NestedTests {
    @Test
    public void thisTestDoesnt() { }
  }
}
Run Code Online (Sandbox Code Playgroud)

Dou*_*mos 9

要运行所有嵌套类,您只需在类名末尾添加“*”即可。就像是:

mvn -Dtest=com.mycompany.test.MyTest\* surefire:test
Run Code Online (Sandbox Code Playgroud)

或者

mvn -Dtest=com.mycompany.test.MyTest* surefire:test
Run Code Online (Sandbox Code Playgroud)