-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
Issue
When running tests written in Kotlin with the '@DataJpaTest' annotation, the application context is started for every class ran, rather then re-using the existing one on the cache.
Versions
Spring Boot - 1.4.1.RELEASE
Kotlin - 1.0.4
Java - 1.8
Details
I am running into an issue when using Kotlin to write my integration tests with @DataJpaTest. When I run all of the tests together, I expect the @DataJpaTest classes to re-use the first application context, however they all end up starting their own. When I tried writing the same basic test in Java, only one application context was started.
Using IntelliJ's Kotlin tools, I decompiled the Kotlin code into Java. Now, when I run the new Java tests, it behaves in the same way as Kotlin, which was expected. The difference I noticed is the Kotlin @Metadata annotation. When I comment out the values d1 and d2 in the annotation (or the whole annotation), the new Java tests work properly, only starting one application context.
Also, switching the Kotlin tests to use the @SpringBootTest worked with only one application context starting.
I tried making a post on Kotlin's discussion board, but failed to get a response. I'm hoping your team may be able to provide insight on how I can work around this issue in Kotlin. Or if it is something that can be resolved on your end.
I've included sample Kotlin code, sample Java code, as well as attached a sample project used to replicate my issue.
Kotlin
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@DataJpaTest
open class KotlinDataJpaTest2 {
@Test
open fun contextLoads() {
println("dataJpaTest2")
}
}Java (generated from Kotlin decompiler)
import kotlin.Metadata;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
@Metadata(
mv = {1, 1, 1},
bv = {1, 0, 0},
k = 1 ,
d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\b\u0017\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0003\u001a\u00020\u0004H\u0017¨\u0006\u0005"},
d2 = {"Lcom/example/JavaDataJpaTest3;","","()V","contextLoads","","test sources for module AppContextDataJpaTest"}
)
public class JavaDataJpaTest3 {
@Test
public void contextLoads() {
String var1 = "dataJpaTest2";
System.out.println(var1);
}
}