【发布时间】:2016-11-12 15:08:23
【问题描述】:
我现在正在尝试 Gradle 和 jUnit5。一切正常,除了我无法运行特定的 jUnit 测试。当我右键单击一个测试类时,没有出现“运行'SampleTest'”选项。
我有最新版本的 IntelliJ (2016.1.3) Ultimate。这是我的build.gradle 文件:
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 中一样)。下面是一个测试示例:
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 文件:
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
【问题讨论】:
标签: java intellij-idea gradle junit junit5