DEV Community

loading...

JUnit5 Unit Testing, Test events were not received.

raquezha
android dev by day, musician by night
・2 min read

The Problem

I came from Junit4 testing, and I want to try Junit5 and started writing some unit tests and I'm curious why I received this message:

Test events were not received.

After some digging I've found out more detailed errors:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':splash:testDebugUnitTest'.
> No tests found for given includes: [com.raquezha.heograpiya.splash.SampleTestTest](filter.includeTestsMatching)
Enter fullscreen mode Exit fullscreen mode

So I google on how to solve the problem and stumble on this site gradle.org site

I just have to add this code to enable JUnit Platform to run my tests:

Gradle DSL (build.gradle)

test {
    useJUnitPlatform()
}
Enter fullscreen mode Exit fullscreen mode

Kotlin DSL (build.gradle.kts)

tasks.named<Test>("test") {
    useJUnitPlatform()
}
Enter fullscreen mode Exit fullscreen mode

But after adding those code another problem occurred:

A problem occurred evaluating project ':app'.
"> Could not find method test()"

I've added this because maybe for some this is the solution but unfortunately this did not work for me. If above did not work for you can try the solution below.

Solution

After digging a little because all of my searches were not working I stumble upon this stackoverflow post it turns out I'm missing one import!

I just add this missing dependency:

testImplementation 'org.junit.jupiter:junit-jupiter:5.8.0-M1'
Enter fullscreen mode Exit fullscreen mode

And after successful gradle sync, tadah!
image

I hope this helps someone fix this noob error.

--

Here's what I'm using

  1. Android Studio Arctic Fox | 2020.3.1 Canary 15
  2. Gradle 7.0
  3. Kotlin 1.5.0
  4. Dependencies
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.0-M1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.0-M1'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.0-M1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.0-M1'
Enter fullscreen mode Exit fullscreen mode

Discussion (0)