繁体   English   中英

如何在 Spring Boot for Kotlin 中为应用程序 class 编写测试

[英]How to write a test for the Application class in Spring Boot for Kotlin

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class GetThingsDoneApplication

fun main(args: Array<String>) {
    runApplication<GetThingsDoneApplication>(*args)
}

我正在尝试将我的项目的测试覆盖率提高到 100%。 但我不知道如何为main(args: Array<String>)编写测试

我想测试 Class 和方法应该是这个样子:

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

@ContextConfiguration(classes = [GetThingsDoneApplication::class])
class GetThingsDoneApplicationIntegrationTest {

    @Test
    fun contextLoads() {
        main(doSomething())
    }

    private fun doSomething(): Array<String> {
        return arrayOf<String>()
    }
}

现在的问题是,我的assertThat应该是什么样子才能有 100% 的测试覆盖率?

这对我有用

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import kotlin.test.assertTrue

@SpringBootTest
@ActiveProfiles("test")
internal class SomeAppTest {

    @Test
    fun contextloads() {
        assertTrue { true }
    }
}

当你说Failed to load ApplicationContext时,你能提供整个堆栈跟踪吗?

实际上,获得 100% 的最有效方法是从测试覆盖率中排除Application.kt文件,如此处所述IntelliJ - exclude some classes (packages) from test coverage report

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM