3

I have a Spring Boot Kotlin project with a database. I have JUnit tests that use an embedded database.

Here is the @Entity I want to test

@Entity
@Table(name = "client")
data class Client(
        @Id
        @Column(name = "id")
        val id: UUID,

        @Column(name = "modified_on")
        var modifiedOn: OffsetDateTime = OffsetDateTime.now(),
)

When I run the test using gradle (./gradlew clean test) the tests succeed.

When I run the test through IntelliJ (right click > run all tests), I get an error:

org.springframework.orm.jpa.JpaSystemException: No default constructor for entity

The docs on the Kotlin website make it clear - using the plugin 'kotlin-jpa' will automatically create no-arg constructors for classes annotated with @Entity

If I go to Project Structure > Facets > Kotlin > Kotlin main or test > Compiler Plugins, I can see two plugins: kotlin-allopen and kotlin-noarg. They appear to be configured correctly, and kotlin-noarg specifies @Entity.

I've specified the plugin in build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'org.jetbrains.kotlin.jvm' version '1.3.41'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.3.41'
    // kotlin-jpa plugin allows no-arg constructors, e.g. on @Entity
    id 'org.jetbrains.kotlin.plugin.jpa' version '1.3.41'
}

How can I get JUnit tests that depend on the kotlin-noarg plugin to run in IntelliJ?


Related:

These don't have a resolution. They are also not specific to JUnit and IntelliJ - the plugin works otherwise.

The resolution here doesn't work.

aSemy
  • 5,485
  • 2
  • 25
  • 51

1 Answers1

1

My issue with this was trying to use Kotlin's new IR backend:

tasks.withType<KotlinCompile> {
        kotlinOptions { 
            jvmTarget = "1.8"
            useIR = true
        }
    }

Removing useIR = true solved the plugin issue

LeoColman
  • 6,950
  • 7
  • 34
  • 63