Gradle: kotlin tests can't see java files in test

Viewed 1290

java 8, gradle 5, kotlin 1.2.71, junit 5.3.2, intellij 2018.03 december 17, keeping kotlin and java files in the same src dir the file structure is:

.
├── build.gradle
├── gradle
│   └── wrapper...
│       
├── gradlew
├── gradlew.bat
└── src
    ├── main
    │   ├── kotlin
    │   │   └─ ...
    │   └── resources
    │       └── application.yml
    └── test
        └── kotlin
            └────── xxx
                    └── shop
                        └── order
                            ├── Junit5Kotlin.kt
                            ├── Junit5Test.java
                            ├── OrderITest.kt
                            ├── ProductITest.kt
                            └── Util.java

from intellij ('run all tests' clicked on 'test' dir) all works correctly. from ./gradlew test test kotlin classes don't see java test util classes:

xxx.shop.order.Manual > should return client error on bad input FAILED
    java.lang.NoClassDefFoundError: xxx/shop/order/Util
        at xxx.shop.order.Manual.before(ProductITest.kt:229)

        Caused by:
        java.lang.ClassNotFoundException: xxx.shop.order.Util
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
            at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
            ... 1 more

and it's not a compile time error but runtime error as other tests finished successfully

potentially important parts of my build.gradle:

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'xxx.shop'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

test {
    useJUnitPlatform()
}
1 Answers

You have to put your java files in java dir or else they will not be compiled.

└── src
    ├── main
    │   ├── kotlin
    │   │   └─ ...
    │   ├── java
    │   │   └─ ...
    │   └── resources
    │       └── application.yml
   └── test
        └── kotlin
            └────── xxx
                    └── shop
                        └── order
                            ├── Junit5Kotlin.kt
                            ├── OrderITest.kt
                            ├── ProductITest.kt
                            └── Util.java
            java
            └────── xxx
                    └── shop
                        └── order
                            ├── Junit5Test.java

And add java plugin to gradle

apply plugin: 'java'

EDIT: OK, if you want it all to be in kotlin dir, try to add this to you build.gradle:

apply plugin: 'java'
sourceSets.main.java.srcDirs = ['src/main/kotlin']
sourceSets.test.java.srcDirs = ['src/test/kotlin']

This way it will run java compiler before kotlin one so you will have this .class files.

Related