在IntelliJ中运行jUnit 5测试的Gradle项目 [英] Gradle project running jUnit 5 tests in IntelliJ

查看:273
本文介绍了在IntelliJ中运行jUnit 5测试的Gradle项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在尝试Gradle和jUnit5。一切正常,但我无法运行特定的jUnit测试。当我右键点击一个测试类时,Run'SampleTest'选项不会出现。



我拥有最新版本的IntelliJ(2016.1.3)Ultimate。这是我的 build.gradle 文件:

 存储库{
mavenCentral()
}

应用插件:'java'

version ='1.0.0-SNAPSHOT'

jar {
baseName ='test-project'
}

依赖关系{
testCompile组:'org.junit.jupiter',名称:'junit-jupiter-api' ,版本:'5.0.0-M1'
}

项目结构是标准一个(如Maven)。这里是一个测试的例子:

  package com.test; 

导入org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SampleTest {
@Test public void sampleTest(){
int test = 1;
Assertions.assertTrue(test == 1);


我缺少什么?



编辑:



似乎Gradle并没有选择我的测试。当我到 build / reports / tests / index.html 时,它表示0测试。



最终编辑:



继@ dunny的回答之后,我做了一切工作。我修改了 build.gradle 这样的文件:

  buildscript {
存储库{
mavenCentral()
}
依赖项{
classpath'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'


$ b存储库{
mavenCentral()
}

应用插件:'java'
apply插件:'org.junit.platform.gradle.plugin'

version ='1.0.0-SNAPSHOT'

jar {
baseName ='test-project '
}

依赖项{
testCompile组:'org.junit.jupiter',名称:'junit-jupiter-api',版本:'5.0.0-M1'
testCompile组:'org.junit.platform',名称:'junit-platform-runner',版本:'1.0.0-M1'
testCompile组:'junit',name:'junit' ,版本:'4.12'
testRuntime组:'org.junit.jupiter',名称:'junit-jupiter-engine',版本:'5.0.0-M1'
}

test {
testLogging {
events'started','passed'
}
}

在IntelliJ中,我随后打开了Gradle窗口,并单击了刷新所有gradle项目按钮,以刷新库。



然后在我的测试类中,我在类声明的顶部添加了 @RunWith(JUnitPlatform.class)



当我做一个 gradle build 时,结果可以在这里找到: build \test-results\junit-platform\TEST-junit -Jupiter.xml

解决方案

IntelliJ 2016.1.3不支持JUnit 5测试。但是,您可以添加注释 @RunWith(JUnitPlatform.class),它将以JUnit 4兼容模式执行测试(您仍然可以使用所有JUnit 5功能)。请参阅 http://junit.org/junit5 / docs / current / user-guide /#running-tests-junit-platform-runner 获取更多信息。



对于Gradle,您需要包含Gradle JUnit 5插件来支持:

$ $ p $ $ c $ buildscript {
repositories {
mavenCentral()
//以下仅在需要使用SNAPSHOT发行版时才需要。
// maven {url'https://oss.sonatype.org/content/repositories/snapshots'}
}
依赖关系{
classpath'org.junit.platform: junit-platform-gradle-plugin:1.0.0-M1'
}
}

apply plugin:'org.junit.platform.gradle.plugin'

请参阅 http://junit.org/junit5/docs/current/user-guide/#running-tests-build


I am trying both Gradle and jUnit5 right now. Everything works fine except that I cannot run a specific jUnit test. The "Run 'SampleTest'" option does not appear when I right-click a test class.

I have the latest version of IntelliJ (2016.1.3) Ultimate. Here is my build.gradle file:

repositories {
    mavenCentral()
}

apply plugin: 'java'

version = '1.0.0-SNAPSHOT'

jar {
    baseName = 'test-project'
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
}

The project structure is the standard one (like in Maven). And here is an example of a test:

package com.test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SampleTest {
  @Test public void sampleTest() {
    int test = 1;
    Assertions.assertTrue(test == 1);
  }
}

What am I missing?

EDIT:

It seems that Gradle is not picking up my test either. When I go to build/reports/tests/index.html, it indicates 0 test.

FINAL EDIT:

Following @dunny's answer, here is what I did to make everything work. I modified my build.gradle file like this:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'
    }
}

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

version = '1.0.0-SNAPSHOT'

jar {
    baseName = 'test-project'
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M1'
}

test {
    testLogging {
        events 'started', 'passed'
    }
}

In IntelliJ, I then opened the Gradle window, and clicked on the "refresh all gradle projects" button, to refresh the libraries.

Then in my test class, I added @RunWith(JUnitPlatform.class) on top of the class declaration.

And when I do a gradle build, the results are available here: build\test-results\junit-platform\TEST-junit-jupiter.xml

解决方案

IntelliJ 2016.1.3 doesn't have support for JUnit 5 tests. You can however add the annotation @RunWith(JUnitPlatform.class), which would execute your test in a JUnit 4 compatibility mode (you can still use all JUnit 5 features). See http://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner for more information.

For Gradle you need to include the Gradle JUnit 5 plugin to enable support:

buildscript {
    repositories {
        mavenCentral()
        // The following is only necessary if you want to use SNAPSHOT releases.
        // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'
    }
}

apply plugin: 'org.junit.platform.gradle.plugin'

See http://junit.org/junit5/docs/current/user-guide/#running-tests-build

这篇关于在IntelliJ中运行jUnit 5测试的Gradle项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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