我正在使用this blog post来配置Sping Boot 项目的集成测试,但是我在声明源代码集方面遇到了很大的困难,我也找到了this post on StackOverflow,但是我想我已经走得更远了。
我的项目结构是
project
|_ src
|_ main
| |_ kotlin
| |_ resources
|_ testIntegration
| |_ kotlin
| |_ resources
|_ test
| |_ kotlin
| |_ resources
|_ build.gradle.kts
|_ ... other files
以及 * 构建.gradle.kts*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
idea
kotlin("jvm")
id("org.springframework.boot") version "2.0.5.RELEASE"
id("org.jetbrains.kotlin.plugin.spring") version "1.2.71"
}
fun DependencyHandlerScope.springBoot(module: String) = this.compile("org.springframework.boot:spring-boot-$module:2.0.5.RELEASE")
fun DependencyHandlerScope.springBootStarter(module: String) = this.springBoot("starter-$module")
dependencies {
springBoot("devtools")
springBootStarter("batch")
springBootStarter("... spring boot dependencies")
compile("... more dependencies")
testCompile("... more test dependencies")
}
val test by tasks.getting(Test::class) {
useJUnitPlatform { }
}
kotlin {
sourceSets {
val integrationTest by creating {
kotlin.srcDir("src/testIntegration/kotlin")
resources.srcDir("src/testIntegration/resources")
}
}
}
val integrationTestCompile by configurations.creating {
extendsFrom(configurations["testCompile"])
}
val integrationTestRuntime by configurations.creating {
extendsFrom(configurations["testRuntime"])
}
val testIntegration by tasks.creating(Test::class) {
group = "verification"
testClassesDirs = kotlin.sourceSets["integrationTest"].kotlin
}
idea {
module {
testSourceDirs.addAll(kotlin.sourceSets["integrationTest"].kotlin.srcDirs)
testSourceDirs.addAll(kotlin.sourceSets["integrationTest"].resources.srcDirs)
}
}
我想我的方向差不多是对的。至少它不再抛出异常了:)
当我运行 testIntegration 任务时,我得到了以下输出:
Testing started at 12:08 ...
12:08:49: Executing task 'testIntegration'...
> Task :project:compileKotlin UP-TO-DATE
> Task :project:compileJava NO-SOURCE
> Task :project:processResources UP-TO-DATE
> Task :project:classes UP-TO-DATE
> Task :project:compileTestKotlin UP-TO-DATE
> Task :project:compileTestJava NO-SOURCE
> Task :project:processTestResources UP-TO-DATE
> Task :project:testClasses UP-TO-DATE
> Task :project:testIntegration
BUILD SUCCESSFUL in 2s
5 actionable tasks: 1 executed, 4 up-to-date
12:08:51: Task execution finished 'testIntegration'.
此外,IntelliJ不能将testIntegration目录识别为Kotlin包。
5条答案
按热度按时间ggazkfy81#
多亏了KotlinSlack频道的一些帮助,我终于能够弄明白了。首先我必须升级到Gradle版本4. 10. 2。
有关详细信息,请查看Gradle的以下两页:
然后,我只需为integrationTests创建源集
这对于Java来说很好,但是由于我使用的是Kotlin,所以必须添加一个额外的
withConvention
Package 器在文档中,他们只放置了
runtimeClasspath += output + compileClasspath
,但我添加了sourceSets["test"].runtimeClasspath
,这样我就可以直接使用测试依赖项,而不是为integrationTest
任务声明新的依赖项。一旦创建了sourceSet,就需要声明一个新任务
在此之后,测试仍然没有运行,但这是因为我使用的是JUnit4。因此,我只需要添加
useJUnitPlatform()
,这将使其成为最终代码u0sqgete2#
我不喜欢withConvention的使用,也不喜欢Kotlinsrc目录的设置方式,所以在查看了这里和这里的两个gradle文档后,我得出了以下结论:
在使用
kotlin {
和使用变量进行新的integrationTestTask时,我更喜欢不那么冗长的风格。lo8azlld3#
从Gradle 5.2.1开始,请访问https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
qncylg1j4#
下面是你可以参考的git repo:enter link description here
希望这对你有帮助。:)
qhhrdooz5#
Gradle有一个名为
Declarative Test Suite
的专用功能支持这种情况:更多信息:www.example.comhttps://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests