running test suite is not working in junit5 through maven
@SelectPackages和@SelectClasses标签没有使用maven test命令进行解析。虽然它在IDE中工作正常。 甚至我在pom.xml中尝试使用tag。
这是代码片段:
PaymentServiceTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package xyz.howtoprogram.junit5.payment; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @RunWith(JUnitPlatform.class) public class PaymentServiceTest { @Test public void doPaymentZeroAmount() { assertEquals(1, 1); } } |
UserFeatureSuiteTest.java
1 2 3 4 | @RunWith(JUnitPlatform.class) @SelectPackages("xyz.howtoprogram.junit5.payment") public class UserFeatureSuiteTest { } |
它没有在包下运行任何测试用例。 虽然它下面有一个测试用例。
xyz.howtoprogram.junit5.payment
- > PaymentServiceTest.java
1 2 | Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest. |
即使我尝试更改pom.xml,例如添加'include'标记。
的pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.jupiter.version>5.0.0-M2</junit.jupiter.version> <junit.vintage.version>4.12.0-M2</junit.vintage.version> <junit.platform.version>1.0.0-M2</junit.platform.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <includes> <include>**/UserFeatureSuiteTest.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> <version>${junit.vintage.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> junit-platform-runner</artifactId> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> </dependency> <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> </dependency> </dependencies> |
下面是最终的POM,现在正在运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.howtoprogram</groupId> junit5-test-suite-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.jupiter.version>5.0.0-M2</junit.jupiter.version> <junit.vintage.version>4.12.0-M2</junit.vintage.version> <junit.platform.version>1.0.0-M2</junit.platform.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <includes> <include>**/*SuiteTest.java</include> </includes> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> |
所以输出是:
1 2 3 4 5 6 7 8 | T E S T S ------------------------------------------------------- Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines INFO: Discovered TestEngines with IDs: [junit-jupiter] Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines INFO: Discovered TestEngines with IDs: [junit-jupiter] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest |
很抱歉让我感到困惑......我只是仔细看看你的代码并意识到你正试图混合两个概念。
您的测试代码使用JUnit 5 @Test进行注释。因此,此代码必须使用junit-jupiter-engine运行。该引擎不支持声明性套件,但是从maven-surefire-plugin读取配置。
@RunWith是一个JUnit4注释,在junit-jupiter-engine中被完全忽略。我不认为它会导致测试失败,但它也不会启用任何JUnit 5测试。在此上下文中,您放置在套件类中的@SelectPackages注释将仅帮助确定运行哪些JUnit 4测试 - 但您没有。
通过JUnit平台运行测试时不支持
从用户指南:
Annotating a class with
@RunWith(JUnitPlatform.class) allows it to be run with IDEs and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.
因此,如果希望
或者,您可以直接执行测试,例如
默认情况下,Maven在查找要运行的测试时使用以下命名约定:
-
Test* -
*Test -
*TestCase
您的测试类不遵循这些约定。您应该重命名它或配置Maven Surefire插件以使用其他模式进行测试类。
另一件可能导致Maven无法工作的事情是所有测试都应该在以下文件夹中:
/my_application/src/test/java/MyFirstExampleTest.java
在这里你可以看到一个很好的问题,可以概括你的问题,从我的答案的某些部分。你应该看看它。
编辑
在这里,您可以看到一个示例,说明您的pom.xml应该如何:
的pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.codefx.demo</groupId> junit-5</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>5.0.0-M3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgs> -parameters</arg> </compilerArgs> </configuration> </plugin> <plugin> maven-surefire-plugin</artifactId> <version>2.19</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.0.0-M3</version> </dependency> <dependency> <!-- contains the engine that actually runs the Jupiter-tests --> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.0.0-M3</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> |
As you can see in this configuration file you specify:
- Dependency: you need a test scoped dependency of JUnit in order to run tests
- In the build section you will add the surefire plugin that will run your tests along with its dependencies