【问题标题】:IntelliJ can't find tests if package name exists in src/main/java如果包名存在于 src/main/java 中,IntelliJ 无法找到测试
【发布时间】: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/&lt;package&gt; 和测试代码src/test/java/&lt;package&gt; 分开...
  • 我的意思是我有 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


【解决方案1】:

所以事实证明存在一些问题(其中一些可能与特定问题无关,但仍然是最佳实践)。 对我有帮助的是:

  1. 通过导入 junit-bom 整合 JUnit 的依赖管理
  2. 正在将 maven-surefire-plugin 升级到最新版本
  3. 从 maven-surefire-plugin 中删除不必要的配置(我的配置标签现在是空的)
  4. 在 IntelliJ 中重新加载项目(右键单击项目 -> Maven -> 重新加载项目)

    非常感谢 khmarbaiseKathrin Geilmann 的帮助:)

【讨论】:

    猜你喜欢
    • 2012-02-17
    • 2016-11-19
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2012-02-08
    • 2015-08-18
    相关资源
    最近更新 更多