Java Junit5 与 IntelliJ 和 Gradle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45462987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Junit5 with IntelliJ and Gradle
提问by LazerBanana
Trying to migrate my project to java8 + Junit5 using IntelliJ 2017.2
尝试将我的项目迁移到 java8 + Junit5 使用 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-M6
和junit-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 语法。
采纳答案by LazerBanana
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 启动器存在错误
OXYGENif 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 JUnit4tests 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 JUnit5tests 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'
}
}
回答by sweetfa
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>
回答by Alabi Temitope
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'
回答by André Machado
I have to change the version of JUnit from 5.4.0 for 5.3.2 and it works like a charm.
我必须将 JUnit 的版本从 5.4.0 更改为 5.3.2,它就像一个魅力。