3

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.

Sergei Rybalkin
  • 3,337
  • 1
  • 14
  • 27
  • Have you looked at https://stackoverflow.com/a/46033685/256245 ? – Fabricio Lemos Jul 18 '18 at 17:01
  • Possible duplicate of [How to use "Functional bean definition Kotlin DSL" with Spring Boot and Spring WebFlux?](https://stackoverflow.com/questions/45935931/how-to-use-functional-bean-definition-kotlin-dsl-with-spring-boot-and-spring-w) – Fabricio Lemos Jul 20 '18 at 15:58

2 Answers2

6

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.

pixel
  • 24,905
  • 36
  • 149
  • 251
Sergei Rybalkin
  • 3,337
  • 1
  • 14
  • 27
0

You can even have multiple ApplicationContextInitializer and provide a comma separated list of them in your properties(order matters). This can be useful if you used Initializer in your main code and would want to override some of your beans using, again, bean definition dsl.

yuranos
  • 8,799
  • 9
  • 56
  • 65