Search code examples
springspring-bootspring-webfluxspring-boot-testspring-bean-dsl

Functional bean definition Kotlin DSL - add initializer to single test class?


I have a Spring Boot test that is using Redis and I would like to load only Redis related beans for that test.

I have a function that defines beans:

fun BeanDefinitionDsl.redisBeans() {
    bean {
    // ...
    }
}

And I would like to have only those beans added to the single test class.

Is there a way to do so without adding bean initializer to application properties in the test resource folder?


Solution

  • It turns out that I can have normal Junit test (not a @SpringBootTest) and get context built with initializers like:

    SpringApplicationBuilder()
            .bannerMode(Mode.OFF)
            .initializers(MyCustomInitializer())
            .sources(DummyConfig::class.java)
            .properties(
                mapOf(
                    "spring.autoconfigure.exclude" to "..."
                )
            )
            .web(WebApplicationType.NONE)
            .build()
            .run()
            .getBean(MyBeanThatIWantToTest::class.java)
    // ...
    
        @EnableAutoConfiguration
        @Configuration
        class DummyConfig