【问题标题】:java.lang.NoSuchMethodError: Lorg/junit/platform/commons/function/Try;java.lang.NoSuchMethodError: Lorg/junit/platform/commons/function/Try;
【发布时间】:2019-08-06 17:57:42
【问题描述】:

我想使用最新的 JUnit 版本:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>

但是当我编译代码时,我得到了这个异常:

ar 15, 2019 5:42:45 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;

你知道我该如何解决这个问题吗?我使用 Java 11。

【问题讨论】:

  • 您正在使用的实现 Jupiter 包的代码?
  • 您可能需要 junit-platform-commons,因为这是 Junit-Jupiter-api 和 apiguardian-api 的依赖项,最后是 opentest4j,然后对于 junit-jupiter-engine,您需要 junit-platform-engine。这就是所有的依赖关系。

标签: java unit-testing junit junit-jupiter


【解决方案1】:

这个错误是由 Spring 的依赖管理 starter poms 和你的 pom.xml 配置引起的。 Spring-boot-starter-test 正在获取您需要排除的 JUnit4。

如果你使用这个 pom.xml 配置,你应该能够修复它:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
              <exclusion>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
              </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2020-12-01
    • 1970-01-01
    • 2020-05-20
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2017-11-11
    相关资源
    最近更新 更多