108

I have a Kotlin project and when I run my JUnit tests, I can't see tests execution result in IntelliJ and get this message instead:

test events were not received

I'm using this setup:

macOS Mojave

Intellij CE 2019.2

JDK 11.0.3

Kotlin 1.3.50

Gradle 5.2.1

JUnit 4.12

Can you help me?

Héctor
  • 24,444
  • 35
  • 132
  • 243

26 Answers26

121

For me, the Same issue was occurring, below changes worked for me. IntelliJ Version I am using: 2019.2.2
In IntelliJ IDE, Go to

File -> Settings ->Build,Execution, Deployment -> Build Tools -> Gradle

here in the Run test using: dropdown selected option was: Gradle(default) changed it to IntelliJ IDEA

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
Prat S
  • 1,221
  • 1
  • 7
  • 4
  • 1
    Thanks - I had the same issue with Scalatest tests (also using Gradle) and this fixed it. – Ryan Dec 16 '19 at 05:50
  • After doing this, how do I run the tests exactly? – birgersp Mar 25 '20 at 16:15
  • @birgersp open a test class, left click on the green arrow at the left hand side of the class or one of its methods and select _Run test..._. The default shortcut `Ctrl`+`Shift`+`F10` will run the current test or class, depending on where the text cursor is located. – Marco Lackovic Dec 07 '20 at 10:10
  • This didn't directly solve the problem for me, but it did improve the diagnostics, giving a better message ("no tests found") which enabled me to solve the problem. – Michael Kay Apr 12 '21 at 11:23
  • 1
    Encountered this error after an IntelliJ update - this answer resolved the issue for me. IntelliJ IDEA 2022.1.2. – Frank Jul 08 '22 at 08:49
  • Doesn't work with 2020.3.1 version of IntelliJ IDE, these options are not available in gradle settings. Had to manually edit app:build.gradle – Vishnu Satheesh Oct 28 '22 at 13:29
  • It seems like this issue is back on Intellij 2023.1.4 Ultimate Edition for Mac OSX. This answer helped me fix it – Marcello Grechi Lins Aug 03 '23 at 17:29
62

Update to 2019.2.2 or later, which contains the fix for the related issue.

A workaround is to run the tests using IntelliJ IDEA instead of Gradle by changing the delegation option.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • IntelliJ IDEA 2019.2.2 is now released. https://blog.jetbrains.com/idea/2019/09/intellij-idea-2019-2-2-is-here/ – anishpatel Sep 06 '19 at 23:25
  • The delegation workaround worked for me but I still had the problem in Intellij v2019.2.3 CE running on Windows 10 - for a Java project running Junit Tests. – robbie70 Oct 20 '19 at 19:18
  • 4
    The same at 2019.2.3 – Zon Nov 25 '19 at 04:57
  • 1
    Same for 2019.2.4 (Ultimate) – Frank Nov 26 '19 at 14:44
  • 1
    IntellijIDEA tells me everything is up to date (31 August 2020( and I'm getting this message. The link to running the tests via IntellijIDEA instead of Gradle doesn't seem to lead to anything useful. How do I resolve this? – digitig Aug 31 '20 at 02:08
  • Disable delegation in [the IDE settings](https://i.stack.imgur.com/ef4Jf.png). – CrazyCoder Aug 31 '20 at 05:09
  • I have had this problem with Kotest tests, but not with pure JUnit 5 tests. After upgrade Kotest to version 4.3.2 I have no more it. – Viktor Sirotin Feb 22 '21 at 15:10
59

When trying to work out this problem for myself, I discovered JUnit 4 worked, but JUnit 5 reports "Tests were not received." Per petrikainulainen.net I found my solution.

Even though Gradle 4.6 (and obviously all newer versions) has a native support for JUnit 5, this support is not enabled by default. If we want to enable it, we have to ensure that the test task uses JUnit 5 instead of JUnit 4.

When I added the following code to Gradle, JUnit 5 worked.

test {
    useJUnitPlatform()
}
Jack J
  • 1,514
  • 3
  • 20
  • 28
  • 4
    This is the only answer that worked in my case; using IntelliJ IDEA 2020.2.3 – Jonathan E. Landrum Nov 12 '20 at 23:29
  • 3
    Even with this setting, it sometimes doesn't work, commenting out `useJUnitPlatform()` line, running the tests and then uncommenting it back seems to help though. – tsobe Nov 22 '20 at 19:49
  • 1
    I need to do this when migrating to Spring Boot v2.x, that also migrates JUnit 4 to JUnit 5 – sebasira Dec 31 '20 at 21:29
38

For anyone that is still facing this -
check if you have any compiler errors in the Build tab.

FazoM
  • 4,777
  • 6
  • 43
  • 61
  • 1
    In my case, I had a compilation error due to a missing constructor parameter I didn't notice (because it was in a different file than the one I tried to run the tests in). I would expect the IDE to simply show me that error instead of pretending the tests are executing but it didn't happen, unfortunately. – Andrzej Zabost Sep 22 '21 at 12:25
  • I had a compilation error as well. – Vishnu Singhal Feb 10 '22 at 15:40
16

In my case I had an invalid Gradle JVM configuration (configured jdk was removed from filesystem).

To Fix it had to change Gradle JVM on File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM.

It was red due to an invalid JDK path.

IntelliJ version: 2019.2.3

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Nacho Silva
  • 240
  • 1
  • 6
4

I often have to delete build and out folder to fix this problem ..

GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
4

For those who still might have this problem using IntelliJ & Kotlin:

Add the following lines to the build.grade.kts. I hope it helps some of you.

dependencies {
    testImplementation(kotlin("test-junit5"))
    testImplementation(platform("org.junit:junit-bom:5.7.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}

PS: without adding the task. before test {...} I was getting the following error!

the function invoke() is not found
aSemy
  • 5,485
  • 2
  • 25
  • 51
moonkin
  • 353
  • 1
  • 10
3

make sure you are using the correct @Test annotation which is from org.junit.Test; Sometimes while auto importing it can get the annotation from some other library. for ex - org.junit.jupiter.api.Test;

Shivam
  • 169
  • 1
  • 3
3

For me 'resetting' the Gradle setting worked.

enter image description here

Farid Khan
  • 131
  • 4
3

For Junit 5 tests add this in your app-level build.gradle file under android{} block.

testOptions {
    unitTests {
        all {
            useJUnitPlatform()
        }
    }
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Sriraksha
  • 459
  • 1
  • 8
  • 15
2

For a Spring boot, gradle project using IntelliJ, my issue was that Spring Boot currently brings JUnit 5 with it. I had also manually installed JUnit 5 as well.

After deleting compile 'org.junit.jupiter:junit-jupiter-api:5.6.3' in build.gradle my tests began to work again.

wallwalker
  • 591
  • 7
  • 14
  • 27
2

I ran into this issue and it turned out that I had written JUnit 4 tests in a module that was configured for JUnit 5. The solution was to use JUnit 5 imports in my test class:

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
crobicha
  • 1,674
  • 1
  • 16
  • 21
1

I was using facing the same issue even using IntelliJ IDEA 2019.2.4 (top answer said this was fixed on 2019.2.2). Turns out I had to Invalidate Caches / Restart... before trying.

1

Try add

testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")

if your tests use the older org.junit.Test instead of org.junit.jupiter.api.Test

André
  • 53
  • 1
  • 5
1

In my case Gradle offline mode was active on Android studio. Just toggled the offline to online and it started working.

Sujit
  • 10,512
  • 9
  • 40
  • 45
1

This command helped me to fix this issue:

gradle clean
Vadim
  • 125
  • 7
1

What worked for me is to simply delete the test configuration, then recreate it.

Run / Edit Configurations

Then, find the target and delete it with the - icon. One easy way to recreate it is to go to the test class and click the run/debug icon on the left gutter.

RonVe
  • 31
  • 5
0

The problem occurred as I used both following plugins at the same time

plugins
{
  id 'org.springframework.boot' version '2.2.6.RELEASE'
  id 'io.spring.dependency-management' version '1.0.7.RELEASE'
  ...
}

after deletion of the second one, the tests were found and executed.

Olivier Faucheux
  • 2,520
  • 3
  • 29
  • 37
0

Answer

I know of some issues that occur when running Gradle together with the newest JDK versions (13 & 14). If you selected either version 13 or 14 of the JVM as the Gradle JVM try using either 8 or 11.

Switch the Gradle JVM to version 11:

File -> Settings -> Build Execution, etc -> Built Tools -> Gradle -> Gradle JVM

Explanation

I believe that by default Intellij will use the project SDK:

Project strcuture -> Project -> Project SDK

I've had this on several occasions now and picking version 11 of the JVM fixed things for me.

Worth noting is that for most of my project I'm using:

  • Lombok
  • Jackson
  • Lombok plugin (Require annotation processing to be enabled)

I don't remember where I read it but I believe there were some issues with the lombok plugin + gradle + JVM version. I might revisit this answer if I can remember where I found the post about it.

Byebye
  • 934
  • 7
  • 24
0

your most likey not tellinig the system how to run the tests.

on your class header define a test runner like this for example:

@RunWith(MockitoJUnitRunner::class)
class MYTest {//...}

or can define junitX runner , etc

j2emanue
  • 60,549
  • 65
  • 286
  • 456
0

This is the solution worked for me:

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

Switching Gradle JVM to: JAVA_HOME version 11.0.10 worked. 

Before it was Project SDK. enter image description here

0

If you are using Kotlin DSL as your gradle script then adding the following did the trick for me:

tasks.withType<Test> {
    useJUnitPlatform() // Make all tests use JUnit 5
}

Explanation: Above idiomatic Kotlin DSL makes all tasks with type Test to use JUnit 5 platform which is required for test to work.

Farrukh Najmi
  • 5,055
  • 3
  • 35
  • 54
0

I was also struggling, and no answer online I found worked for me, and I eventually realized it was due to a System.exit(0) call. As a result, all tests would silently stop after calling whatever code with the exit call and show that test as "ignored" in IntelliJ.

Nick Botticelli
  • 175
  • 2
  • 6
0

Clearing the gradle cache worked for me;

./gradlew clean test --no-build-cache

If it doesn't work, you can try this one

./gradlew clean test --no-build-cache --no-configuration-cache --rerun-tasks

This solution is mentioned in the JetBrains issue tracker. I just wanted to it put here as a shortcut.

azizunsal
  • 2,074
  • 1
  • 25
  • 33
0

Test methods should not be marked as a private:

@Test fun verify() {...} -valid

@Test private fun verify() {...} - not valid

(kotlin, mockk, android studio)

Andrei K
  • 141
  • 2
  • 9
-2

For me, this is helpful to click "Help -> Check for Updates..."

Help -> Check for Updates...