带有多个源集的Gradle下的JUnit 5 [英] JUnit 5 under Gradle with multiple source sets

查看:375
本文介绍了带有多个源集的Gradle下的JUnit 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我想把JUnit 5用在一起。我已经为单元测试正常工作了。



然而,我确实有多个测试源集 - 另外一个用于验收测试。我正在努力研究如何让JUnit 5在一个任务中运行src / test中定义的单元测试 - 在另一个任务中运行Acceptance Tests - 在acceptanceTestsourceSet中定义并位于src / acceptance - 在另一个任务中任务。



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



build.gradle:

  buildscript {
ext {
jackson_version =2.9.0.pr4
// IntelliJ需要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
}

存储库{
mavenCentral()
jcenter()
}

取决于encies {
classpathcom.github.ben-manes:gradle-versions-plugin:0.15.0
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
classpathorg.jetbrains.kotlin:kotlin-allopen:$ kotlin_version
classpathorg.junit.platform:junit-platform-gradle-plugin:$ junitPlatform_version
classpathorg.springframework.boot :spring-boot-gradle-plugin:$ springBoot_version

}

插件{
idcom.github.ben-manes.versionsversion 0.15.0


套用插件:com.github.ben-manes.versions
套用插件:kotlin
套用插件:kotlin -spring
apply plugin:org.junit.platform.gradle.plugin
apply plugin:org.springframework.boot
apply plugin:war

存储库{
mavenCentral()
maven {urlhttps://jitpack.io}
}

sourceSets {
acceptanceTest {
kotlin {
compileClasspath + = main.output + test.output
runt imeClasspath + = main.output + test.output
srcDir文件('src / acceptance / kotlin')
}
resources.srcDir文件('src / acceptance / resources')
}
}

配置{
acceptanceTestCompile.extendsFrom testCompile
acceptanceTestRuntime.extendsFrom testRuntime
}

依赖关系{
编译com.graphql-java:graphql-java-tools:3.1.3
compilecom.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
}
编译org.jetbrains.kot lin:kotlin-stdlib:$ kotlin_version
compileorg.jetbrains.kotlin:kotlin-reflect:$ kotlin_version
compileorg.slf4j:slf4j-api:$ slf4j_version
compile org.springframework:spring-jdbc:$ spring_version
compileorg.springframework.boot:spring-boot-starter-web:$ springBoot_version
compileorg.springframework.boot:spring-boot -starter-actuator:$ springBoot_version
compileru.yandex.qatools.embed:postgresql-embedded:2.2

runtimech.qos.logback:logback-classic:1.2。 3
runtimeorg.jolokia:jolokia-core:1.3.7
runtimeorg.liquibase:liquibase-core:3.5.3
runtimeorg.postgresql:postgresql: 42.1.3
运行时org.slf4j:jcl-over-slf4j:$ slf4j_version
运行时org.slf4j:jul-to-slf4j:$ slf4j_version

testCompilecom.github.sbrannen:spring-test-junit5:1.0.0.M4
testCompilecom.nhaarman:mockito-kotlin:1.5.0
testCompile(org.jetbrains.kotlin :kotlin-test-junit:$ kotlin_version){
排除组:junit,模块:junit
}
testCompileorg.junit.jupiter:junit-jupiter-api:$ junitJupiter_version

testRuntimeorg。 junit.jupiter:junit-jupiter-engine:$ junitJupiter_version

acceptanceTestCompileorg.springframework.boot:spring-boot-starter-test:$ springBoot_version


任务acceptanceTest(类型:Test){
testClassesDirs = sourceSets.acceptanceTest.output.classesDirs
classpath = sourceSets.acceptanceTest.runtimeClasspath
outputs.upToDateWhen {false}
}

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

任务包装器(类型:包装器){
gradleVersion =4.0
}


解决方案从Gradle 4.6开始,Gradle Java插件具有 JUnit 5支持



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

  apply plugin:'java'

test {
useJUnitPlatform()
}

intTest {
useJUnitPlatform()
}

依赖项{
testCompile(org.junit.jupiter:junit-jupiter-api:5.1.0)
testRuntime(org.junit.jupiter:junit-jupiter-engine:5.1.0)

}


I've got a project I'm putting together that I want to use JUnit 5 for. I've got this working fine for Unit Tests already.

I do however have multiple test source sets - I've got an additional one for Acceptance Tests. I'm struggling to work out how to get JUnit 5 to run the Unit Tests - defined in src/test - in one task and the Acceptance Tests - defined in the "acceptanceTest" sourceSet and located in "src/acceptance" - in another task.

I have previously got this working with JUnit 4 and Cucumber, but the JUnit 5 plugin doesn't seem to want to work like this.

build.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"
}

解决方案

Since Gradle 4.6 the gradle Java plugin has JUnit 5 support

So once you have configured the test source sets, the setup is as simple as follows - for my test and intTest test tasks I just need to configure this to use 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")

}

这篇关于带有多个源集的Gradle下的JUnit 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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