【问题标题】:JUnit is fine via command line, fails in IntelliJJUnit 通过命令行很好,在 IntelliJ 中失败
【发布时间】:2021-05-09 08:17:33
【问题描述】:

我正在从事一个对我来说是新的项目,但已经存在了一段时间。

我尝试使用mvn clean test 从终端运行单元测试,单元测试按预期运行。

不幸的是,我真的可以在其中一些单元测试中使用 IntelliJ 的一些调试帮助。每当我尝试从 IntelliJ 运行单元测试时,都会得到以下输出:

发生内部错误。

org.junit.platform.commons.JUnitException: ID 为“junit-vintage”的 TestEngine 未能发现测试

原因: org.junit.platform.commons.JUnitException:不支持的版本 junit:junit:3.8.1。请升级到 4.12 或更高版本。

根据 pom.xml,我使用的是 JUnit 5.7.0、Mockito 3.7.7,并且(因为我在谷歌上搜索的一些结果表明这可能是 Spring Boot 问题)没有 Spring Boot 的迹象。我也尝试过包含 junit-vintage,因为它在输出中提到,但这似乎也没有什么区别。

【问题讨论】:

  • 您是从 IntelliJ 内的 Maven 插件窗口还是从 IntelliJ 菜单运行测试?使用独立机制来处理库依赖关系。如果您使用 Maven 窗口,它应该与命令行一样工作。
  • 您使用的是哪个版本的 IJ?
  • Mac 上的 IntelliJ 2020.3.1。我一直在从终端选项卡(mvn clean test)或菜单(右键单击->调试 test_x...)运行测试。

标签: java unit-testing intellij-idea junit


【解决方案1】:

你可以尝试添加

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13.1</version>
</dependency>

到你的 pom.xml。也许您将 junit 仅作为传递依赖项,因此选择了 3.8.1。

【讨论】:

  • 我不确定什么是传递依赖,但我对 org.junit.jupiter 组的 3 个不同的 maven 工件有 5.7.0 依赖:junit-jupiter-engine、junit-jupiter-params , 和 junit-jupiter-api.
  • 传递依赖是另一个依赖的依赖。
【解决方案2】:

这是旧的 StackOverflow 诅咒 - 寻找解决方案几个小时,一无所获;在 StackOverflow 上发帖,即使有帮助的人也不知道问题出在哪里,但现在我可以快速从 Google 找到答案。

尽管我之前在我的项目中添加了 junit-vintage,但显然我没有添加正确的工件、版本或其他东西。因为这就是这里的解决方案。

Here is a link 到导致我得到答案的讨论中——添加这个依赖:

    <dependency> 
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>

【讨论】:

    【解决方案3】:

    可以尝试以下几个步骤来解决问题:

    1. 运行 mvn dependency:tree 并检查 junit:junit 的传递依赖项(如果有)。

    2. 在父 pom.xml 中检查 junit-addons(如果存在)并从中排除 junit。

    
    <dependency>
        <groupId>junit-addons</groupId>
        <artifactId>junit-addons</artifactId>
        <version>1.4</version>
        <exclusions>
            <!-- Note: avoid junit 3 as a dependency -->
            <exclusion>
                <artifactId>junit</artifactId>
                <groupId>junit</groupId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多