当使用 Intellij 运行测试时,“未接收测试事件”

我有一个 Kotlin 项目,当我运行 JUnit 测试时,我不能在 IntelliJ 中看到测试执行结果,而是得到以下消息:

未收到测试事件

我正在使用这个设置:

macOS Mojave


Intellij CE 2019.2


JDK 11.0.3


Kotlin 1.3.50


Gradle 5.2.1


JUnit 4.12

你能帮我吗?

76969 次浏览

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.

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

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

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

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.

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()
}

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

Help -> Check for Updates...

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.

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

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.

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

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

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.

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;

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

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

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

For me 'resetting' the Gradle setting worked.

enter image description here

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.

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.

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

testOptions {
unitTests {
all {
useJUnitPlatform()
}
}
}

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