问题再现:
应用 Intellij Idea 开发工具,每次执行单个 Junit5 测试时一切正常。
然而在我的项目目录中,执行 mvn test,或者 Intellij 左边的 Maven 关上后执行 Lifecycle 的 test 时,呈现问题:
查阅文档
Junit5-User Guide-Get Started 给出了一些 Junit5 的例子。
其中,junit5-jupiter-starter-maven pom.xml 中给了我一些提醒!
批改 pom.xml
- 依赖 junit-jupiter-engine
- 新增插件 maven-surefire-plugin
<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.coderead</groupId> <artifactId>test-junit5</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build></project>
参考博客
解决mvn test运行Junit5测试用例时检测不到测试类的问题