Search code examples
springspring-bootunit-testingkotlinlateinit

Kotlin Autowired problems - lateinit (obv)


I'm relatively new to kotlin and I can't get my head around spring autowiring

kotlin.UninitializedPropertyAccessException: lateinit property applicationContext has not been initialized

I got the above error while running this:

@SpringBootTest
internal class MeterResourceAdapterTest () {

    @Autowired
    private lateinit var applicationContext: ApplicationContext

@Test
    fun toMeterReadingResourceList() {
        print(applicationContext.applicationName)
.....//I hope my implementation can be ignored
}

What is going on here? Why, incidentally, is lateinit required, and WHEN does 'it' get initialised?


Solution

  • @Stav Shamir's comment lead me to this resolution.

    I can't say for sure what caused this, but it was almost certainly to do with an older version of junit, I had in my pom.xml. When I created a new project, via the initialisier, then copied that pom's contents ( < dependencies/ > ) into my pom; all went smoothly.