7

我有一个我想使用 JUnit 5 的项目。我已经为单元测试做好了这个工作。

但是,我确实有多个测试源集 - 我还有一个用于验收测试。我正在努力研究如何让 JUnit 5 运行单元测试 - 在 src/test 中定义 - 在一个任务中和验收测试 - 在“acceptanceTest”源集中定义并位于“src/acceptance”中 - 在另一个任务中任务。

我以前使用过 JUnit 4 和 Cucumber,但 JUnit 5 插件似乎不想这样工作。

构建.gradle:

buildscript {
  ext {
    jackson_version = "2.9.0.pr4"
    // IntelliJ needs M4
    junitJupiter_version = "5.0.0-M4"
    junitPlatform_version = "1.0.0-M4"
    kotlin_version = "1.1.3-2"
    slf4j_version = "1.7.25"
    spring_version = "4.3.10.RELEASE"
    springBoot_version = "1.5.4.RELEASE"
    springBootAdmin_version = "1.5.2"

    runAcceptance = System.properties['noAcceptance'] == null
  }

  repositories {
    mavenCentral()
    jcenter()
  }

  dependencies {
    classpath "com.github.ben-manes:gradle-versions-plugin:0.15.0"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatform_version"
    classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version"
  }
}

plugins {
  id "com.github.ben-manes.versions" version "0.15.0"
}

apply plugin: "com.github.ben-manes.versions"
apply plugin: "kotlin"
apply plugin: "kotlin-spring"
apply plugin: "org.junit.platform.gradle.plugin"
apply plugin: "org.springframework.boot"
apply plugin: "war"

repositories {
  mavenCentral()
  maven { url "https://jitpack.io" }
}

sourceSets {
  acceptanceTest {
    kotlin {
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
      srcDir file('src/acceptance/kotlin')
    }
    resources.srcDir file('src/acceptance/resources')
  }
}

configurations {
  acceptanceTestCompile.extendsFrom testCompile
  acceptanceTestRuntime.extendsFrom testRuntime
}

dependencies {
  compile "com.graphql-java:graphql-java-tools:3.1.3"
  compile "com.graphql-java:graphql-spring-boot-starter:3.5.0"
  compile "com.zaxxer:HikariCP:2.6.3"
  compile("de.codecentric:spring-boot-admin-server:$springBootAdmin_version") {
      exclude group: "junit", module: "junit"
  }
  compile("de.codecentric:spring-boot-admin-server-ui:$springBootAdmin_version") {
      exclude group: "junit", module: "junit"
  }
  compile("de.codecentric:spring-boot-admin-starter-client:$springBootAdmin_version") {
      exclude group: "junit", module: "junit"
  }
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
  compile "org.slf4j:slf4j-api:$slf4j_version"
  compile "org.springframework:spring-jdbc:$spring_version"
  compile "org.springframework.boot:spring-boot-starter-web:$springBoot_version"
  compile "org.springframework.boot:spring-boot-starter-actuator:$springBoot_version"
  compile "ru.yandex.qatools.embed:postgresql-embedded:2.2"

  runtime "ch.qos.logback:logback-classic:1.2.3"
  runtime "org.jolokia:jolokia-core:1.3.7"
  runtime "org.liquibase:liquibase-core:3.5.3"
  runtime "org.postgresql:postgresql:42.1.3"
  runtime "org.slf4j:jcl-over-slf4j:$slf4j_version"
  runtime "org.slf4j:jul-to-slf4j:$slf4j_version"

  testCompile "com.github.sbrannen:spring-test-junit5:1.0.0.M4"
  testCompile "com.nhaarman:mockito-kotlin:1.5.0"
  testCompile("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") {
      exclude group: "junit", module: "junit"
  }
  testCompile "org.junit.jupiter:junit-jupiter-api:$junitJupiter_version"

  testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitJupiter_version"

  acceptanceTestCompile "org.springframework.boot:spring-boot-starter-test:$springBoot_version"
}

task acceptanceTest(type: Test) {
  testClassesDirs = sourceSets.acceptanceTest.output.classesDirs
  classpath = sourceSets.acceptanceTest.runtimeClasspath
  outputs.upToDateWhen { false }
}

if (ext.runAcceptance) {
  check.dependsOn acceptanceTest
}
acceptanceTest.mustRunAfter test

task wrapper(type: Wrapper) {
  gradleVersion = "4.0"
}
4

4 回答 4

5

要优化现有答案,您需要将每个Test-type 任务配置为包括useJUnitPlatform(). 在您的具体示例中,这看起来像

task acceptanceTest(type: Test) {
  useJUnitPlatform()
  testClassesDirs = sourceSets.acceptanceTest.output.classesDirs
  classpath = sourceSets.acceptanceTest.runtimeClasspath
  outputs.upToDateWhen { false }
}

您也可以通过以下方式集中执行此操作

tasks.withType(Test) {
    useJUnitPlatform()
}
于 2018-05-14T13:33:18.140 回答
5

从 Gradle 4.6 开始,gradle Java 插件支持 JUnit 5

因此,一旦您配置了测试源集,设置就很简单了 - 对于我的test测试intTest任务,我只需将其配置为使用 JUnit5:

apply plugin: 'java'

test {
    useJUnitPlatform()
}

intTest {
    useJUnitPlatform()
}

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.1.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")

}
于 2018-02-24T17:41:33.140 回答
2

JUnit 5 插件仅创建一个任务 - junitPlatformTest,并将其基于junitPlatform扩展。

如果您想在另一个源集中独立运行验收测试或任何其他类型的测试,您需要创建自己的JavaExec任务,就像插件一样。

参见:https ://github.com/junit-team/junit5/blob/master/junit-platform-gradle-plugin/src/main/groovy/org/junit/platform/gradle/plugin/JUnitPlatformPlugin.groovy#L66

于 2017-08-14T12:20:40.840 回答
1

您可能想要创建这样的任务:

task e2eTest(
        type: JavaExec,
        description: 'Runs the e2e tests.',
        group: 'Verification'
) {
    dependsOn testClasses
    shouldRunAfter test

    classpath = sourceSets.e2e.runtimeClasspath

    main = 'org.junit.platform.console.ConsoleLauncher'
    args = ['--scan-class-path',
            sourceSets.e2e.output.getClassesDirs().asPath,
            '--reports-dir', "${buildDir}/test-results/junit-e2e"]
}
于 2017-09-25T17:24:04.907 回答