java.lang.NoSuchMethodError : Lorg/junit/platform/commons/function/Try;

标签 java unit-testing junit junit-jupiter

我想使用最新的 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。

最佳答案

此错误是由 Spring 的依赖管理启动器 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>

关于java.lang.NoSuchMethodError : Lorg/junit/platform/commons/function/Try;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55186246/

相关文章:

java - 用 Espresso 测试 recyclerView,如何执行点击或断言

java - 不使用 InjectMocks 创建对象会导致 httpClient 出现问题

ruby - 为什么我的 rspec 测试在 Ruby 1.9.2 中比在 1.8.7 中运行得慢?

c# - Moq'ing 多次引发事件

java - JUnit 返回错误,创建 URL 中定义的名称为 'sessionFactory' 的 bean

java - EasyMock JUnit 测试在 setter 方法上抛出错误

java - Maven surefire permgen 空间不足

java - 如何显示我的小程序的个人 Action ?

java - 如果父级不使用 Java 中的 stdout/stderr,为什么进程会挂起?

java - 在没有电子邮件实际出去的情况下运行 Junit 电子邮件测试