cbeust / kobalt

A Kotlin-based build system for the JVM.
Apache License 2.0
428 stars 60 forks source link

KotlinTest NoClassDefFoundError #492

Open davidsowerby opened 5 years ago

davidsowerby commented 5 years ago

Attempting to run tests with KotlinTest causes the error below. I think it is simply because the Kobalt KotlinTestRunner is based on JUnit4. KotlinTest uses JUnit5, from version 3.0.0 I believe, which would have been after you added KotlinTest support at #312

───── kayman-design:compile ───── kayman-design:compileTest ───── kayman-design:test Running tests with JUnit 4


***** ERROR Error: java.lang.NoClassDefFoundError: io/kotlintest/specs/BehaviorSpec


import com.beust.kobalt.project
import com.beust.kobalt.test

val p = project {
    name = "kayman-design"
    group = "com.example"
    artifactId = name
    version = "0.1"

    dependencies {
        compile("org.jetbrains.kotlin:kotlin-runtime:1.2.61")
        compile("org.jetbrains.kotlin:kotlin-stdlib:1.2.61")
    }

    dependenciesTest {
        compile("io.mockk:mockk:1.8.6")
        compile("io.kotlintest:kotlintest-runner-junit5:3.1.10")
    }

}
cbeust commented 5 years ago

Can you attach your project, or link to it, so I can reproduce this?

davidsowerby commented 5 years ago

I'll put up a sample project - give me a day or so ...

davidsowerby commented 5 years ago

I've put up a sample project which seems to confirm my suspicion that it is a runner change that is needed.

I also noticed that when the test run fails under these conditions, the build is still reported as successful:

 ╔════════════════════════════╗
 ║ Building kobalt-kotlintest ║
 ╚════════════════════════════╝

───── kobalt-kotlintest:compile ───── kobalt-kotlintest:compileTest ───── kobalt-kotlintest:test Running tests with JUnit 4


***** ERROR Error: java.lang.NoClassDefFoundError: io/kotlintest/specs/BehaviorSpec


Thread report ╔════════════════════════════════════════╗ ║ Time (sec) ║ Thread 12 ║ ╠════════════════════════════════════════╣ ║ 0 ║ kobalt-kotlintest ║ ╚════════════════════════════════════════╝ ╔═════════════════════════════════════════════════════════╗ ║ Project ║ Build status║ Time ║ ╠═════════════════════════════════════════════════════════╣ ╚═════════════════════════════════════════════════════════╝ PARALLEL BUILD SUCCESSFUL (0 SECONDS), sequential build would have taken 0 seconds 10:12:35: Task execution finished 'test'.

cbeust commented 5 years ago

There was a bug in how Kobalt detects whit JUnit runner to use (it mistakenly used JUnit 4).

With 1.0.117, JUnit 5 is correctly detected on your project, however, I'm now getting different errors after that. I suspect the way to invoke and build JUnit 5 tests has slightly changed since last time I wrote this code.

Do you have a Gradle build that works on your tests, something I could compare with?

cbeust commented 5 years ago

In the meantime, you can run ./kobaltw --update to update to 117 and try again. You'll see a lot of warnings during discovery and finally a compilation error.

davidsowerby commented 5 years ago

That was quick. I can see the warnings and the compile error in Build.kt. I've put up a Gradle equivalent, but limited both the Gradle and Kobalt builds to KotlinTest3 - just makes things easier. Let me know if you need KotlinTest2

I've also added a failing test to both, and used KotlinTest's in-built assertions as a more realistic scenario. Interestingly these caused a compile error, because the test is still using the KotlinTest 2.0.7 library.

Knowing this sometimes happens in IDEA, I tried re-importing - but that refused to work because of the compile error.