【问题标题】:Junit5 with IntelliJ and GradleJunit5 与 IntelliJ 和 Gradle
【发布时间】:2018-01-09 19:20:42
【问题描述】:

尝试使用IntelliJ 2017.2 将我的项目迁移到 java8 + Junit5

我已经添加了junit-jupiter-api版本5.0.0-M6

junit-platform-launcher 版本1.0.0-M6

项目结构是默认的maven约定src/test/java

找到了几篇关于此的文章,但没有一篇能解决我的问题。

它在控制台中运行良好,我认为这与 IntelliJ 默认的 JUnit Runner 有关,或者我缺少一些依赖项?


当我运行单个测试类时,一切正常,但是当我像以前一样选择目录和 Run all 'Tests' in Java 时,我遇到了一些错误。

WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V

Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;

注意:我还没有迁移任何测试,都是 Junit 4 语法。

【问题讨论】:

  • 调试它的一般方法是在命令行上运行 gradle test。这应该比 IntelliJ 提供更详细的输出。

标签: java intellij-idea junit


【解决方案1】:

添加特定的依赖项可以解决问题。

注意:更新 2017.2.0 以上的 INTELLIJ,因为 JUNIT 启动器存在错误

OXYGEN 如果您使用 Eclipse。


以下依赖项启用 Junit5 参数化测试,可用于代替 DataProvider

"org.junit.jupiter:junit-jupiter-params:5.0.0"
//for JUnit5 parametrized tests.

Junit5 API

"org.junit.jupiter:junit-jupiter-api:5.0.0"
//JUnit5 API

如果您想在不更改语法和导入的情况下运行旧版 JUnit4 测试,则需要。

"org.junit.vintage:junit-vintage-engine:4:12.0"
//for legacy JUnit4 tests

编辑:07/2018 将 Vintage runner 的版本与 jupiter 版本匹配


如果您想使用新语法和导入运行 JUnit5 测试,则需要。

"org.junit.jupiter:junit-jupiter-engine:5.0.0"
//for JUnit5 tests

java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;


启动器

"org.junit.platform:junit-platform-launcher:1.0.0
//to handle default launcher

线程“主”java.lang.NoSuchMethodError 中的异常:org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;


更多信息如何安装JUnit5


从 Gradle 4.6 版开始,不再需要插件 Gradle 原生支持 Junit5,只需执行以下操作: 并且 Vintage runner 的版本现在与 JUnit 5 版本相同。

dependencies {

    testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"

    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junitVersion"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}

test {  
    useJUnitPlatform {
        includeEngines 'junit-jupiter', 'junit-vintage'
    }
}

【讨论】:

  • 2017.2.0也有bug?
  • 找不到参数的方法 test() ... 在哪里放置测试块?
  • 应用了java还是android插件?测试任务通过这些插件来完成。
  • 添加junit-platform-launcher 解决了我的问题。
【解决方案2】:

我使用的配置如下。

仅当您同时使用 junit4 测试时才需要老式引擎依赖项。

只有在使用参数化测试时才需要 jupiter 参数。

<properties>
    <junit.version>5.0.0</junit.version>
</properties>
...
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>4.12.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

【讨论】:

  • 谢谢!你的配置对我有用。 (我使用的是 intelliJ 2018.1)
【解决方案3】:

我必须将 JUnit 的版本从 5.4.0 更改为 5.3.2,它就像一个魅力。

【讨论】:

  • 与 JUnit 5.5.2 相同的问题。
  • 5.5.1 也一样。
  • 与 5.6.2 相同
【解决方案4】:

我在公司环境中使用 IntelliJ 和 jupiter 时遇到问题。 我能够运行'mvn test',但在 IntelliJ 中启动测试提示'IntelliJ failed to resolve junit platform launcher 1.5 2'

配置 HTTP 代理为我解决了这个问题:

设置 -> 外观和行为 -> 系统设置 -> HTTP 代理 -> 手动代理配置

【讨论】:

    【解决方案5】:

    如果遇到 android studio

    无法解析 org.junit.jupiter:junit-jupiter-api

    转到您的文件..\AndroidStudioProjects\Pets\app\build.gradle

    在依赖部分

    输入这两行代码

     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.0.0-M4'
    

    【讨论】:

      【解决方案6】:

      我在 junit 5.6.1 上遇到了同样的问题,将它升级到 5.7.0 修复了它:

      testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
      testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
      

      【讨论】:

        【解决方案7】:

        如果您在代理服务器后面设置并且 maven 全新安装工作正常(但不是 intellij),那么很可能它与代理有关。这是为我解决的问题。选择代理自动检测。

        【讨论】:

          【解决方案8】:

          Official docs:

          Maven Surefire 和 Maven Failsafe 可以运行基于 JUnit 4 的测试 只要您配置测试范围,就可以与 Jupiter 测试一起 依赖于 JUnit 4 和 JUnit Vintage TestEngine 实现类似于以下。

          <!-- ... -->
          <build>
              <plugins>
                  <plugin>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.22.2</version>
                  </plugin>
                  <plugin>
                      <artifactId>maven-failsafe-plugin</artifactId>
                      <version>2.22.2</version>
                  </plugin>
              </plugins>
          </build>
          <!-- ... -->
          <dependencies>
              <!-- ... -->
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.13</version>
                  <scope>test</scope>
              </dependency>
              <dependency>
                  <groupId>org.junit.vintage</groupId>
                  <artifactId>junit-vintage-engine</artifactId>
                  <version>5.7.2</version>
                  <scope>test</scope>
              </dependency>
              <!-- ... -->
          </dependencies>
          <!-- ... -->
          

          【讨论】:

            猜你喜欢
            • 2017-01-14
            • 2018-02-24
            • 1970-01-01
            • 1970-01-01
            • 2018-11-24
            • 2023-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多