Search code examples
spring-bootkotlinspring-boot-test

How to use Kotlin beans dsl initializer in SpringBootTest


I have a simple application with several beans declared with kotlin beans dsl:

@SpringBootApplication
class App

val beans = beans {
    bean<A>()
}

fun main(args: Array<String>) {
    runApplication<MatchmakerApp>(*args) {
        addInitializers(beans)
    }
}

@RestController
class AppController(val a: A) {
    // some code
}

class A

and I have an integration test:

@RunWith(SpringRunner::class)
@SpringBootTest
class AppControllerTest {
    @Test
    fun dummyTest() {
        assert(true)
    }
}

Launching this test I'm getting

UnsatisfiedDependencyException: Error creating bean with name appController 
Caused by: NoSuchBeanDefinitionException: No qualifying bean of type 'A' available:`

It seems beans initializer was not invoked during SpringBootTest context creation.

What do we need to add kotlin bean dsl initializer in SpringBootTest?

The general way with @ContextConfiguration(initializers = ...) does not work here, because it looks for classes.


Solution

  • add FuBeansInitializer in the same package with App class in the test directory:

        class FuBeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
            override fun initialize(context: GenericApplicationContext) = beans.initialize(context)
        }
    

    add context.initializer.classes into test application.properties:

        context.initializer.classes=path.to.FuBeansInitializer
    

    As a result, there will be nothing modified in the source files. And tests will work fine.