Search code examples
kotlinmicronautkotlintest

How to use MicronautTest with Kotlintest to inject beans while testing ? in Kotlin


How to inject the following into Test, as no constructor args are allowed and its failed to initialise the injected beans

@MicronautTest
class ApplicationTest:StringSpec() {

    @Inject
    lateinit val embeddedServer:EmbeddedServer;

    @Inject
    lateinit val dataSource:DataSource

    init{
        "test something"{
            //arrange act assert
        }
    }
}

Solution

  • You need to specify Project config by creating an object that is derived from AbstractProjectConfig, name this object ProjectConfig and place it in a package called io.kotlintest.provided. KotlinTest will detect it's presence and use any configuration defined there when executing tests. as per the documentation https://github.com/kotlintest/kotlintest/blob/master/doc/reference.md#project-config

    object ProjectConfig :AbstractProjectConfig() {
    override fun listeners() = listOf(MicornautKotlinTestExtension)
    override fun extensions() = listOf(MicornautKotlinTestExtension)
    }