首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >kotlintest测试spring引导应用程序如何?

kotlintest测试spring引导应用程序如何?
EN

Stack Overflow用户
提问于 2018-11-13 08:47:25
回答 1查看 5.1K关注 0票数 1

Spring引导应用程序的集成测试总是首先启动web服务器。

spring引导测试的最简单测试如下所示,如何使用kotlintest来迁移它?

代码语言:javascript
复制
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ReportApplicationTests {

    @Test
    fun `Server can be launched`() {
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-14 08:41:12

下面是我如何设置它的方法:首先,确保引用JUnit 5而不是4,例如,我在我的build.gradledependencies部分得到了这个

代码语言:javascript
复制
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

代码语言:javascript
复制
test {
    useJUnitPlatform()
}

然后,在您的集成测试类中有以下内容(注意listeners的重写,如果没有它,它就无法工作):

代码语言:javascript
复制
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测试测试风格,例如FunSpecShouldSpec等。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53277045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档