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

280 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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