JUnit 通过命令行没问题,在 IntelliJ 中失败

JUnit is fine via command line, fails in IntelliJ

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

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

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

Internal Error occurred.

org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-vintage' failed to discover tests

Caused by: org.junit.platform.commons.JUnitException: Unsupported version of junit:junit: 3.8.1. Please upgrade to version 4.12 or later.

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

你可以尝试添加

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

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

这是 Whosebug 的老诅咒——花上好几个小时寻找解决方案,却一无所获; post 在 Whosebug 上,即使有帮助的人也不知道问题出在哪里,但现在我可以从 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>

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

  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>