Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin-jpa plugin not creating empty constructors for Junit tests

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:

  • kotlin-jpa plugin not generating default constructor
  • no default constructor for JPA entity with Kotlin even with noarg plugin

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

  • Kotlin with JPA: default constructor hell

The resolution here doesn't work.

like image 657
aSemy Avatar asked Mar 04 '26 23:03

aSemy


1 Answers

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

like image 115
LeoColman Avatar answered Mar 06 '26 14:03

LeoColman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!