0
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%. But I don't know how to write a test for main(args: Array<String>)

I guess the Test Class and method should look something like this:

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?

Fahri Can
  • 431
  • 3
  • 13
  • 1
    `arrayOf()` create an array of size `0`. You should instead do `return arrayOf(anyWord)` – amanin Jan 22 '23 at 15:38
  • Okay and how would my assert would look like? – Fahri Can Jan 22 '23 at 15:43
  • I do not know. Personnally, I would create a @SpringBootTest annotated class, with an empty `contextLoads() {}` method. Then, Spring would try to start the app automatically when launching the test, and produce an error if the app could not be launched. – amanin Jan 22 '23 at 15:50
  • When I use `@SpringBootTest` then I get `Failed to load ApplicationContext` – Fahri Can Jan 22 '23 at 16:29
  • But I can start the test with `@ContextConfiguration(classes = [GetThingsDoneApplication::class])` – Fahri Can Jan 22 '23 at 16:40

2 Answers2

0

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?

AndrewL
  • 2,034
  • 18
  • 18
  • Okay, with `@SpringBootTest` and `@ActiveProfiles("test")` it works now, but still I need to somehow write a test for `main()` method to get 100% test coverage. Any ideas on that? – Fahri Can Jan 22 '23 at 17:19
  • 1
    By using `main()` you are bootstrapping the application in a different way. Whilst not your question, my opinion is that, I don't think achieving 100% test coverage is a worthwhile goal. It will blind you to creating useless tests to attempt to cover every line of source and not really test functionality that is complex/likely to break. Many articles discuss this, e.g. https://roelofjanelsinga.com/articles/100-test-coverage-why-or-why-not/ I recommend a risk-based strategy to what should be tested. Testing Spring Boot instantiation is very valuable, but that alternate main invocation?? – AndrewL Jan 22 '23 at 23:25
0

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

Fahri Can
  • 431
  • 3
  • 13