首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在jUnit 5中运行IntelliJ 5测试的分级项目

在jUnit 5中运行IntelliJ 5测试的分级项目
EN

Stack Overflow用户
提问于 2016-07-10 16:12:31
回答 3查看 12K关注 0票数 12

我现在正在尝试Gradle和jUnit5。除了我不能运行特定的jUnit测试之外,一切都很好。当我右键单击一个测试类时,"Run‘SampleTest“选项不会出现。

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

代码语言:javascript
复制
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'
}

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

代码语言:javascript
复制
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);
  }
}

我遗漏了什么?

编辑:

看来Gradle也没有拿起我的测试。当我进入build/reports/tests/index.html时,它表示0测试。

最终编辑:

下面是@dunny的回答,下面是我为使一切正常运转所做的一切。我修改了我的build.gradle文件如下:

代码语言:javascript
复制
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'
    }
}

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

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

当我做gradle build时,结果可以在这里获得:build\test-results\junit-platform\TEST-junit-jupiter.xml

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-10 16:41:30

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插件以启用支持:

代码语言:javascript
复制
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'

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

票数 10
EN

Stack Overflow用户

发布于 2016-07-21 16:35:44

最新的Idea 2016.2现在支持JUnit 5框架。您可以直接运行JUnit5测试,而不需要junit-gradle-plugin。请看INTELLIJ的新理念是什么?。在将您的想法升级到这个新版本之后,您可以创建一个gradle项目并执行以下步骤来测试如何运行JUnit 5测试。

  • build.gradle 应用插件:'java‘compileTestJava { sourceCompatibility = 1.8 targetCompatibility = 1.8 }存储库{ mavenCentral() }依赖项{mavenCentral//注意:如果用以下// testRuntime替换了testRuntime依赖项(“org.jun it.jupiter:junit-木星-引擎:5.0.0-M1“)//这一测试将失败。}
  • 在测试源文件夹中创建一个类FirstJUnit5Test 导入org.junit.jupiter.api.Test;导入静态org.junit.jupiter.api.Test公共类FirstJUnit5Test { @Test (){ assertEquals(2,1+ 1);}
  • 在左侧项目窗格中右键单击此测试类,然后选择"Run‘FirstJUnit5Test“。

更新

对于IDEA 2016.3.3及更高版本,可以将dependencies配置简化为:

代码语言:javascript
复制
dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}
票数 13
EN

Stack Overflow用户

发布于 2016-07-14 09:49:05

您必须使用使用JUnit 4运行程序运行测试,因为IntelliJ 2016.1.3没有JUnit 5测试运行程序。

如果从文档中链接的示例pom.xml ( https://github.com/junit-team/junit5-samples/blob/r5.0.0-M1/junit5-maven-consumer/pom.xml )开始,则执行以下两项操作。

再添加一个依赖项

代码语言:javascript
复制
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
    </dependency>

然后,使您的测试类成为公共,并使用@RunWith(JUnitPlatform.class)对其进行注释。

您的IDE现在将将该类识别为测试,并将提供集成。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38293901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档