简体   繁体   English

Junit5 与 IntelliJ 和 Gradle

[英]Junit5 with IntelliJ and Gradle

Trying to migrate my project to java8 + Junit5 using IntelliJ 2017.2尝试使用IntelliJ 2017.2将我的项目迁移到 java8 + IntelliJ 2017.2

I have added junit-jupiter-api version 5.0.0-M6我添加了junit-jupiter-api版本5.0.0-M6

and junit-platform-launcher version 1.0.0-M6junit-platform-launcher版本1.0.0-M6

Project structure is a default maven convention src/test/java项目结构是默认的 maven 约定src/test/java

Found a couple articles about this but none of them did solve my issue.找到了几篇关于此的文章,但没有一篇确实解决了我的问题。

It runs nicely in a console, I presume this is something to do with the IntelliJ default JUnit Runner, or I am missing some dependencies?它在控制台中运行良好,我认为这与 IntelliJ 默认的 JUnit Runner 有关,或者我缺少一些依赖项?


When I Run a single test class all works fine but when I select the directory and Run all 'Tests' in Java like I used to do then I encounter few errors.当我运行单个测试类时,一切正常,但是当我像以前一样选择目录并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;

Note : I have not migrated any tests yet, all are Junit 4 syntax.注意:我还没有迁移任何测试,都是 Junit 4 语法。

Adding specific dependencies solve the problem.添加特定的依赖项可以解决问题。

NOTE: UPDATE INTELLIJ ABOVE 2017.2.0 AS THERE WAS A BUG WITH THE JUNIT LAUNCHER注意:更新 2017.2.0 以上的 INTELLIJ,因为 JUNIT 启动器存在错误

OXYGEN if you using eclipse.如果您使用日食,则为氧气


Below dependency enables Junit5 parametrized tests which can be used instead of a DataProvider .下面的依赖项启用 Junit5 参数化测试,可用于代替DataProvider

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

Junit5 API . Junit5 API

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

Needed if you want to run legacy JUnit4 tests without changing the syntax and imports.如果您想在不更改语法和导入的情况下运行遗留JUnit4测试,则需要。

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

EDIT: 07/2018 Match the version of the vintage runner to the jupiter version编辑:07/2018 将复古赛跑者的版本与木星版本相匹配


Needed if you want to run JUnit5 tests with new syntax and imports.如果要使用新语法和导入运行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; java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;


Launcher .启动器

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

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


Additional info how to install JUnit5附加信息如何安装JUnit5


Since version 4.6 for Gradle, there is no need for plugins anymore Gradle supports Junit5 natively just do: And the version of the vintage runner is now same as the JUnit 5 version.从 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'
    }
}

The configuration I use is below.我使用的配置如下。

The vintage engine dependency is only required if you are using junit4 tests as well.仅当您还使用 junit4 测试时才需要老式引擎依赖项。

The jupiter params is only required if using parameterised tests.仅在使用参数化测试时才需要 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>

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

I had a problem using IntelliJ and jupiter in a corporate environment.我在企业环境中使用 IntelliJ 和 Jupiter 时遇到问题。 I was able to run 'mvn test', but starting tests in IntelliJ prompts 'IntelliJ failed to resolve junit platform launcher 1.5 2'我能够运行“mvn test”,但在 IntelliJ 中启动测试提示“IntelliJ 无法解析 junit 平台启动器 1.5 2”

Configuring a HTTP Proxy fixed this for me:配置 HTTP 代理为我解决了这个问题:

Settings -> Appearance & Behavior -> Systems Settings -> HTTP Proxy -> Manual proxy configuration设置 -> 外观和行为 -> 系统设置 -> HTTP 代理 -> 手动代理配置

for android studio if you encounter android studio 如果你遇到

Unable to resolve org.junit.jupiter:junit-jupiter-api无法解析 org.junit.jupiter:junit-jupiter-api

go to your file ..\\AndroidStudioProjects\\Pets\\app\\build.gradle转到您的文件..\\AndroidStudioProjects\\Pets\\app\\build.gradle

at the dependencies section在依赖项部分

input these two lines of code输入这两行代码

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

I had same problem with junit 5.6.1 and bumping it to 5.7.0 fixed it:我在 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"

If you are setting behind a proxy server and maven clean install works fine(but not intellij), then most likely, it's proxy related.如果您设置在代理服务器后面并且 maven clean install 工作正常(但不是 intellij),那么很可能是代理相关的。 Here is what fixed it for me.这是为我解决的问题。 Choosing proxy auto detection.选择代理自动检测。

在此处输入图片说明

Official docs : 官方文档

Maven Surefire and Maven Failsafe can run JUnit 4 based tests alongside Jupiter tests as long as you configure test scoped dependencies on JUnit 4 and the JUnit Vintage TestEngine implementation similar to the following. 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> <!-- ... -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM