Search code examples
spring-bootkotlindependency-injectionjunit4

Kotlin Spring Boot Unit Test - adding in @TestExecutionListeners doesn't inject dependencies


I'm trying to use the Flyway Test Extensions library, and one of its instructions was to add in:

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, 
                         FlywayTestExecutionListener.class })

And so in Kotlin I have something along the lines of:

 @RunWith(SpringRunner::class)
 @TestExecutionListeners(DependencyInjectionTestExecutionListener::class, 
                         FlywayTestExecutionListener::class )
 class MyControllerTest {
     @Autowired
     lateinit var dataSource : DataSource
 }

But for some reason, when I try to run a test in that class, I get an error saying that the lateinit property has not been initialized.

Is there something special I need to have/do in order to get this working?


Solution

  • Well, I was able to finally find 1 other post that helped me resolve it: How to persist enums as ordinals with Spring Boot and Cassandra?

    The annotation I needed was:

    @TestExecutionListeners(
        listeners = [FlywayTestExecutionListener::class],
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
    )