【发布时间】:2023-01-30 17:11:28
【问题描述】:
我正在尝试向我们的系统引入单元测试,但遇到了 Junit 找不到测试的问题。 我有这3个测试:
当我运行模块中的所有测试时:
它找到 X 和 Y 测试,但没有找到 Z:
3者的区别仅在于包名:
- 包
com.exlibris.x(XTest) 在项目中不存在 - 包
com.exlibris.core.infra.svc.api.flags(YTest) 存在于项目的不同模块中(即输出到不同的 jar 文件) - 包
com.exlibris.repository.web.mms.publishing(ZTest) 存在于 src/main/java 下的同一模块中
我的 pom.xml 具有以下依赖项(继承自父 pom):
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
编辑:这些是我的运行配置
【问题讨论】:
-
首先使用 junit-bom 文件...并使用最新版本的 surefire-plugin...(更多详细信息:youtu.be/NVvMzy0Lin0)另请参阅真实示例项目:github.com/khmarbaise/youtube-videos/tree/main/episode-2 btw。不清楚你的意思:
with Junit not finding test?? -
对于现有的包,您必须清楚地将生产代码
src/main/java/<package>和测试代码src/test/java/<package>分开... -
我的意思是我有 3 个测试类,但它只运行其中 2 个的测试。如果我删除那 2 个并运行测试,我会得到“未找到测试”
-
@khmarbaise 无论如何,我用 junit-bom 替换了 dependencyManagement 中的依赖项,并将 surefire 从 3.0.0-M4 升级到 3.0.0-M8,我遇到了同样的问题。
-
直接用maven运行可以吗?如果不是,则可能是您的 pom 有问题,如果 maven 执行了所有测试但 intellij 没有执行,则将项目刷新(或重新导入)到 intellij 中可能会有所帮助。
标签: java maven intellij-idea junit junit-jupiter