Skip to content

@DataJpaTest Issues w/ Tests Written in Kotlin #7101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
qtica opened this issue Oct 5, 2016 · 3 comments
Closed

@DataJpaTest Issues w/ Tests Written in Kotlin #7101

qtica opened this issue Oct 5, 2016 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@qtica
Copy link

qtica commented Oct 5, 2016

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);
    }
}

Sample Project

kotlin-and-datajpatest.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 5, 2016
@philwebb philwebb added this to the 1.4.2 milestone Oct 6, 2016
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 6, 2016
@philwebb
Copy link
Member

philwebb commented Oct 6, 2016

The MergedContextConfiguration class is the thing that acts as the cache key. My guess is that something in the class is using the class annotations in the equals method.

@philwebb
Copy link
Member

philwebb commented Oct 6, 2016

The root cause is ImportsContextCustomizer.ContextCustomizerKey which is collecting all class level annotations (including the implicit ones added by Kotlin)

@philwebb
Copy link
Member

philwebb commented Oct 6, 2016

Probably the easiest fix is to filter kotlin.* annotations. We should probably filter java.* as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants