Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle does not find my tests with Kotlin and JUnit 5

I have written some basic unit tests with Kotlin and Junit 5. Unfortunately, when I run them from Intellij IDEA, the tests are not found by Gradle. I am getting the message "No tests found for given includes: [de.mhaug.phd.btcwallet.BasicTests]" from Gradle and the message "Test event not received" from Intellij.

Interestingly, running it from the commandline with "./gradlew :clean :test" reports a successful build. However, my first test is obviously red, so this shows that Gradle did not execute it.

I already tried running it with more verbose output but nothing helpful showed up. Here is a minimal (not) working example:

package de.mhaug.phd.btcwallet  import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Test  class BasicTests {   @Test   fun hey() {     assertEquals(3,1+1)   }    @Test   fun hey2() {     assertFalse(3==1+1)   } } 

This is my build.gradle:

buildscript {     ext.kotlin_version = '1.2.10'      repositories {         mavenCentral()     }     dependencies {         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"     } }  group 'de.mhaug.phd' version '1.0-SNAPSHOT'  apply plugin: 'kotlin'  repositories {     mavenCentral() }  dependencies {     compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"     compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'     testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.2'     testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.2' //    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '' //    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.2' }  compileKotlin {     kotlinOptions.jvmTarget = "1.8" } compileTestKotlin {     kotlinOptions.jvmTarget = "1.8" } 

How can I make Intellij/Gradle execute my tests?

like image 406
icehawk Avatar asked Dec 25 '17 14:12

icehawk


People also ask

How does Gradle find tests?

Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

Does Gradle work with Kotlin?

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.


2 Answers

No, the first comment of @JB Nizet has pointed it out: add test.useJUnitPlatform() alongside your JUnit 5 dependencies. It is the way.

http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle

test {     useJUnitPlatform() } 

or:

dependency {     ...     // junit 5     testImplementation ('org.junit.jupiter:junit-jupiter-api')     testCompile ('org.junit.jupiter:junit-jupiter-params')     testRuntime ('org.junit.jupiter:junit-jupiter-engine')     testCompile ('org.mockito:mockito-junit-jupiter')     test.useJUnitPlatform() // fix "test events not received" bug in IDEA } 

Upvote his comment, not this answer!

like image 117
WesternGun Avatar answered Sep 21 '22 08:09

WesternGun


I think your particular issue of tests not being run was or would be solved by updated everything to the latest version (Gradle, Kotlin, IntelliJ, etc.). Everything is moving really fast at the moment so keeping up to date is a must.

About the 'test events were not received'

As far as I know this is sort of expected. The point is, when using Gradle tasks, either trust your tests were run or figure out a way to display per-test logging.

You need to make a distinction when 'executing the gradle task' or 'running tests using IntelliJ' (e.g. by clicking the gutter icon next to a test).

Running from IntelliJ.

IntelliJ integrates wonderfully with Gradle, in the sense that if you run from IntelliJ, then it will not only run the tests (via Gradle?) but also show some nice test results. All works well with JUnit 5, Kotlintest, Spek, using normal Gradle or Gradle Kotlin DSL or whatever you use.

Shameless self-advert: JUnit 5 and Gradle (also shows Gradle test logging): How to use JUnit 5 with Gradle? and JUnit 5, Kotlintest and Gradle Kotlin DSL: How to run kotlintest tests using the Gradle Kotlin DSL?

Running the Gradle task.

However, when you run a Gradle task using the Gradle tool window, you will see as you mentioned 'test events were not received'. I think this is because you just run a Gradle task which ran the test, and that does not have anything to do with IntelliJ per se, it doesn't integrate in that it reports back results - it just prints to the command line.

Compare for example with running the Kotlin. When you use the Gradle task, it will output in build/. But when you run the main method using the gutter icon, so using IntelliJ, it will output in out/. These are two different things running.

like image 40
PHPirate Avatar answered Sep 20 '22 08:09

PHPirate