@SelectPackages 不适用于 Maven CLI
@SelectPackages not working with Maven CLI
I 运行 mvn clean package
和所有其他测试 运行 除了套件测试。我正在 运行 同时进行 Junit 4 和 5 测试 - @Rules 测试 运行 和 @RegisterExtension/@ExtendWith 测试也 运行。不确定为什么套件不会 运行 - 有什么想法吗?
代码如下:
测试套件
@SelectPackages("com.company.platform.test.suite")
@IncludeClassNamePatterns({"^.*Suite$"})
public class SuiteTest {
}
SuiteTest
正在调用测试
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestingSuite {
@Test
public void add_2_plus_1(){
Assertions.assertThat(2 + 1).isEqualTo(3);
}
}
Pom
<junit-jupiter-engine.version>5.5.0</junit-jupiter-engine.version>
<junit-platform-runner.version>1.5.0</junit-platform-runner.version>
<!-- ellided -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform-runner.version}</version>
</dependency>
<!-- ellided -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
Maven,Java 和 OS 详细信息
mvn11 --version Sun Jul 21 14:43:24 EDT 2019
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.5.4/libexec
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /Users/me/Downloads/jdk-11.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"
所以我复制了你的问题。当运行宁
mvn clean test
我得到了以下输出(以及许多其他输出):
Juli 23, 2019 8:17:30 VORM. org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
WARNING: Ignoring test class using JUnitPlatform runner: com.example.project.SuiteTest
所以我查阅了DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder
的代码,发现了下面的注释:
/**
* Customization of {@link AnnotatedBuilder} that ignores classes annotated
* with {@code @RunWith(JUnitPlatform.class)} to avoid infinite recursion.
*/
private static class DefensiveAnnotatedBuilder extends AnnotatedBuilder {...}
这强烈建议在通过平台机制 运行 时忽略使用 @RunWith(JUnitPlatform.class)
的套件。您可能会问 junit 5 团队是否有明确的方法来关闭该安全机制,但我猜有 none.
我建议您扩展 maven-surefire-plugin 的配置以模仿您在套件中所做的事情:
@SelectPackages("com.company.platform.test.suite")
@IncludeClassNamePatterns({"^.*Suite$"})
可以翻译成
<include>com.company.platform.test.suite/*Suite.java</include>
虽然我没有测试它。
I 运行 mvn clean package
和所有其他测试 运行 除了套件测试。我正在 运行 同时进行 Junit 4 和 5 测试 - @Rules 测试 运行 和 @RegisterExtension/@ExtendWith 测试也 运行。不确定为什么套件不会 运行 - 有什么想法吗?
代码如下:
测试套件
@SelectPackages("com.company.platform.test.suite")
@IncludeClassNamePatterns({"^.*Suite$"})
public class SuiteTest {
}
SuiteTest
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestingSuite {
@Test
public void add_2_plus_1(){
Assertions.assertThat(2 + 1).isEqualTo(3);
}
}
Pom
<junit-jupiter-engine.version>5.5.0</junit-jupiter-engine.version>
<junit-platform-runner.version>1.5.0</junit-platform-runner.version>
<!-- ellided -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform-runner.version}</version>
</dependency>
<!-- ellided -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
Maven,Java 和 OS 详细信息
mvn11 --version Sun Jul 21 14:43:24 EDT 2019
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.5.4/libexec
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /Users/me/Downloads/jdk-11.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"
所以我复制了你的问题。当运行宁
mvn clean test
我得到了以下输出(以及许多其他输出):
Juli 23, 2019 8:17:30 VORM. org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
WARNING: Ignoring test class using JUnitPlatform runner: com.example.project.SuiteTest
所以我查阅了DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder
的代码,发现了下面的注释:
/**
* Customization of {@link AnnotatedBuilder} that ignores classes annotated
* with {@code @RunWith(JUnitPlatform.class)} to avoid infinite recursion.
*/
private static class DefensiveAnnotatedBuilder extends AnnotatedBuilder {...}
这强烈建议在通过平台机制 运行 时忽略使用 @RunWith(JUnitPlatform.class)
的套件。您可能会问 junit 5 团队是否有明确的方法来关闭该安全机制,但我猜有 none.
我建议您扩展 maven-surefire-plugin 的配置以模仿您在套件中所做的事情:
@SelectPackages("com.company.platform.test.suite")
@IncludeClassNamePatterns({"^.*Suite$"})
可以翻译成
<include>com.company.platform.test.suite/*Suite.java</include>
虽然我没有测试它。