Spring引导应用程序的集成测试总是首先启动web服务器。
spring引导测试的最简单测试如下所示,如何使用kotlintest来迁移它?
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ReportApplicationTests {
@Test
fun `Server can be launched`() {
}
}发布于 2018-11-14 08:41:12
下面是我如何设置它的方法:首先,确保引用JUnit 5而不是4,例如,我在我的build.gradle的dependencies部分得到了这个
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
testImplementation "io.kotlintest:kotlintest-extensions-spring:3.1.10"
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.1.10'
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.1"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.3.1"还将此添加到build.gradle中
test {
useJUnitPlatform()
}然后,在您的集成测试类中有以下内容(注意listeners的重写,如果没有它,它就无法工作):
import org.springframework.boot.test.context.SpringBootTest
import io.kotlintest.spring.SpringListener
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [MyApplication::class])
class MyTestStringSpec : StringSpec() {
override fun listeners() = listOf(SpringListener)
init {
// Tests go in here
}
}显然,您可以将StringSpec替换为任何其他Kotlin测试测试风格,例如FunSpec、ShouldSpec等。
https://stackoverflow.com/questions/53277045
复制相似问题