[英]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.