JUnit 5和Mockito [英] JUnit 5 and Mockito

查看:123
本文介绍了JUnit 5和Mockito的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 14和JUnit 5中,如果我在我的构建中添加了嘲笑-junit-jupiter,即使在任何测试中都没有使用嘲笑,尝试运行mvn测试时也会引发错误.不用mockito-junit-jupiter一切都很好.除Maven之外,所有依赖项均为最新版本.

With Java 14 and JUnit 5, if I add mockito-junit-jupiter to my build, an error is thrown when trying to run mvn test even without using mockito in any tests. Wiithout mockito-junit-jupiter everything is okay. All dependencies are the latest versions except Maven.

抛出的错误是

    # Created at 2020-04-15T16:34:33.816
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
...
Caused by: java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ReflectiveInvocationContext
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.<clinit>(ClassBasedTestDescriptor.java:81)


    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-commons</artifactId>
    <version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-api -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-api</artifactId>
    <version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.6.2</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>3.3.3</version>
</dependency>

推荐答案

在混合JUnit 5和Mockito时,Maven依赖项的顺序似乎很重要. Mockito依赖项必须紧随JUnit 5的依赖项.也许它仅是mockito-junit-jupiter的依赖项,但是我尝试将它们组合在一起.

The order of Maven dependencies seems to be important when mixing JUnit 5 and Mockito. The Mockito dependencies MUST come after the ones for JUnit 5. Maybe it is only the one for mockito-junit-jupiter, but I try to group them toegether.

这篇关于JUnit 5和Mockito的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆