【问题标题】:Gradle project running jUnit 5 tests in IntelliJ在 IntelliJ 中运行 jUnit 5 测试的 Gradle 项目
【发布时间】: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


    【解决方案1】:

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

    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

    【讨论】:

    • IntelliJ 2016.2 现在支持 JUnit5。只需使用@Jean-FrançoisBeauchef 编辑到问题中的 build.gradle 文件作为解决方案。
    【解决方案2】:

    你必须run your tests with the JUnit 4 runner,因为 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,请执行以下两件事。

    再添加一个依赖项

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
    

    然后将你的测试类公开并用@RunWith(JUnitPlatform.class)注释它。

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

    【讨论】:

    • 谢谢,但除了 pom.xml 部分,dunni 已经说过一切,但我没有使用 Maven。我正在使用 Gradle。无论如何,谢谢。
    • @Jean 并将测试类公开。没有它,JUnit5 可以工作,而 JUnit4 不行。对于 Gradle 用户来说,将依赖项声明切换为 Gradle 语法应该是微不足道的。
    • 但是我的测试课是公开的吗?
    • 如果您将 Junit5 与 Junit5 运行器一起使用,则不必...
    • 嗯,我明白了。但我试图了解我所做的事情有什么问题。不过还是谢谢。
    【解决方案3】:

    最新的 Idea 2016.2 现在支持 JUnit 5 框架。您可以在没有 junit-gradle-plugin 的情况下直接运行 JUnit5 测试。请参阅WHAT'S NEW IN INTELLIJ IDEA。将您的 Idea 升级到这个新版本后,您可以创建一个 gradle 项目并执行以下步骤来测试如何运行 JUnit 5 测试。

    • build.gradle

      apply plugin: 'java'
      
      compileTestJava {
          sourceCompatibility = 1.8
          targetCompatibility = 1.8
      }
      
      repositories {
          mavenCentral()
      }
      
      dependencies {
          testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M1")
          testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M1")
          //NOTE: if you replaced above testRuntime dependency with following
          //testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M1")
          //this test would fail.
      }
      
    • 在您的测试源文件夹中创建一个类 FirstJUnit5Test

      import org.junit.jupiter.api.Test;
      import static org.junit.jupiter.api.Assertions.assertEquals;
      
      public class FirstJUnit5Test {
          @Test
          void myFirstTest() {
              assertEquals(2, 1 + 1);
          }
      }
      
    • 在左侧项目窗格中右键单击该测试类,然后选择“Run 'FirstJUnit5Test'。您将看到如下结果:
    • 欲了解更多信息,您可以结帐this project from github。

    更新

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

    dependencies {
        testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2018-12-01
      • 2016-06-04
      • 2021-03-15
      • 1970-01-01
      相关资源
      最近更新 更多