【问题标题】:java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run mockito junit5 test with mavenjava.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException 尝试使用 maven 运行 mockito junit5 测试
【发布时间】:2020-05-13 18:09:59
【问题描述】:

我正在使用 JUNIT5 和 Mockito。我想写一个ParameterizedTest。 尝试使用 IntelliJ 运行测试时,我收到以下错误:

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 66 more

下面我要执行的参数化测试

    @Test
    @ParameterizedTest
    @EnumSource(value = Money.Status.class, mode = EnumSource.Mode.EXCLUDE, names = {"deleted", "pending"})
    void testMoneyStates(Money.Status status) {
        System.out.println("Hurray " + status);
    }

我在 IntelliJ 2019.3 下运行它 我在 pom 中有以下依赖项:

        <!-- Junit Mockito Dependencies -->
        <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-junit-jupiter</artifactId>
          <version>RELEASE</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-params</artifactId>
          <version>5.6.0-M1</version>
          <scope>test</scope>
        </dependency>

【问题讨论】:

    标签: mockito junit5


    【解决方案1】:

    你缺少这个依赖:

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    

    【讨论】:

      【解决方案2】:

      我收到错误是因为我的 JUnit 依赖项和 Junit-Jupiter-Params 依赖项位于不同的不兼容版本上。我尝试了多个版本,以下版本对我有用。 mockito-junit-jupiter 3.2.4 版本使用 Junit 5.4.2 版本。

              <dependency>
                  <groupId>org.mockito</groupId>
                  <artifactId>mockito-junit-jupiter</artifactId>
                  <version>3.2.4</version>
                  <scope>test</scope>
              </dependency>
              <dependency>
                  <groupId>org.junit.jupiter</groupId>
                  <artifactId>junit-jupiter-params</artifactId>
                  <version>5.4.2</version>
                  <scope>test</scope>
              </dependency>
      

      【讨论】:

        猜你喜欢
        • 2019-11-24
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-31
        • 1970-01-01
        • 2013-08-23
        相关资源
        最近更新 更多