Kotlin & Springboot 1.4 @DataJpaTest - App Context Reloading

I have a Spring Boot project using the new @DataJpaTest annotation provided in v1.4. I setup all the tests to be written in Kotlin, and noticed an issue when running the tests. For every test class, Spring Boot starts up a new application context. If I write the class in Java, the application context is only started once, and spring re-uses the cached one for the remaining test classes. Also, when using the annotation @SpringBootTest, instead of @DataJpaTest, in both Kotlin and Java the application context is properly re-used.

After looking at the Kotlin bytecode and decompiling it, I noticed the issue is with the @Metadata annotation produced by the Kotlin compiler. If I am to comment out the values d1 and d2, Spring Boot behaves as expected. But when d1 and d2 are present, the application context is re-created.

I’ve attached the sample project I wrote to showcase this issue. If you run the java tests as-is, it should work. However, if you uncomment the d1 and d2 values in the @Metadata annotation on the java test classes, you should see the Spring Boot loading up an application context for every class.

Kotlin version: 1.0.4-release-IJ2016.1-112

kotlin-and-datajpatest.zip (7.0 KB)

Let me know if there is any more information I can provide.

1 Like