簡體   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