简体   繁体   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)
}

I am trying to push my test coverage for my project to 100%.我正在尝试将我的项目的测试覆盖率提高到 100%。 But I don't know how to write a test for main(args: Array<String>)但我不知道如何为main(args: Array<String>)编写测试

I guess the Test Class and method should look something like this:我想测试 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>()
    }
}

The question is now, how should my assertThat look like, to have 100% test coverage?现在的问题是,我的assertThat应该是什么样子才能有 100% 的测试覆盖率?

This works for me这对我有用

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 }
    }
}

When you say Failed to load ApplicationContext can you provide the whole stack trace, please?当你说Failed to load ApplicationContext时,你能提供整个堆栈跟踪吗?

Actually the most effective way to get 100% is to exclude the Application.kt file from the test coverage, like described here IntelliJ - exclude some classes (packages) from test coverage report实际上,获得 100% 的最有效方法是从测试覆盖率中排除Application.kt文件,如此处所述IntelliJ - exclude some classes (packages) from test coverage report

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

相关问题 如何模拟测试 Kotlin Spring Boot 2 应用程序 - how to mock test Kotlin Spring boot 2 application 如何为具有属性依赖性的Spring Boot Application类编写测试用例-无法解析占位符 - How to write test case for Spring boot Application class with properties dependency - Could not resolve placeholder 如何为使用 CommandLineRunner 的 Spring-boot 应用程序的主要 class 编写 groovy 测试 - How to write groovy test for main class of Spring-boot application thast uses CommandLineRunner 如何编写spring boot控制器类单元测试用例 - How to write spring boot controller class Unit test case 如何使用 map 的依赖注入测试 Spring 启动应用程序配置 class? - How to test Spring boot application configuration class with dependency injection of map? 如何测试 Spring-boot 应用程序的主类 - How to test main class of Spring-boot application 如何使用 Spring 引导和 Kotlin 启动 web 应用程序 - How to start the web application with Spring Boot and Kotlin 如何使用 Kotlin 在 Spring 引导中扩展实体 class - How to extend Entity class in Spring Boot with Kotlin Spring Boot,测试主应用类 - Spring Boot, test main application class 如何使用Spring Boot从application.yml读取变量并在Kotlin测试中使用它 - How to read variable from application.yml with spring boot and use it in kotlin test
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM