17
votes

I cannot run tests via Gradle in IntelliJ IDEA because of "No tests found for given includes" error.

How can I fix it?

GradleTests

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class GradleTests {
    @Test
    public void initTest() {
        assertTrue(true);
    }
}

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    //testCompile group: 'junit', name: 'junit', version: '4.12'

    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
}

test {
    useJUnitPlatform()
}

Error:

> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [GradleTests.initTest](filter.includeTestsMatching)

Some notes:

  • Issue is reproduced with both JUnit 4 and 5
  • IntelliJ IDEA 2019.3.3 (Community Edition), Build #IC-193.6494.35, built on February 11, 2020
  • Test is in src/test/java
  • changing runner like Intelij 2019.1 update breaks JUnit tests didn't help
  • without useJUnitPlatform() result is the same
5
What's your Gradle version, and can you run the test from the command line?Ben Watson
And have you tried adding testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")Ben Watson
@BenWatson thanks for help! I read more about JUnit5 last versions and find out that from 5.4.0 it has aggregated artifact "junit-jupiter" (it is more suitable for me) which included both api and engine. It seems that "engine" has been loaded from some other dependency in my main project.Arrovil
So does it work with the aggregate artefact?johanneslink
@johanneslink Yes, I can run my tests with aggregate artifact. I've added and accepted answer to this question.Arrovil

5 Answers

29
votes

I had this error with a similar setup, but couldn't solve it with the previous answers. Resolved it by doing this.

  1. File > Setting (Ctrl+Alt+S)
  2. Build, Execution, Deployment > Build Tools > gradle
  3. Run Tests using: Intellij IDEA

All credit to: https://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/.

11
votes

Thanks to Ben Watson I've found solution. Since JUnit 5.4.0 there is aggregate artifact with both api and engine dependencies. So just adding one dependency to build.gradle resolved this issue.

testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')
2
votes

Another cause of this behaviour when using IntelliJ to edit a Kotlin project is the folders used for the tests, Java classes need to be in a subfolder of java and Kotlin classes in a subfolder of kotlin.

Here's an example of a mini-project I created which demonstrates the folder structure https://github.com/znsio/AutomatedTestingWithKotlin/tree/main/src/test

I found the explanation which is quoted below together with the link to the source:

"My problem was in single path to kotlin and java tests. So kotlin tests are in root/src/test/kotlin/package and they are running fine with gradle :cleanTest :test and java tests must be in root/src/test/java/package. Otherwise neither compileTestKotlin nor compileTestJava will not find java tests to compile." https://discuss.kotlinlang.org/t/not-able-to-run-unit-tests-on-kotlin-multiplatform-project/15779/7

0
votes

Gradle is case sensitive in choosing its selector. See here You may need to change "GradleTests" to "gradleTests"

0
votes

Gradle has no idea where to look for tests. Add this to your app build.gradle file root(Not inside android or dependencies closure):

tasks.withType(Test) {
    useJUnitPlatform()

    testLogging {     // This is for logging and can be removed.
        events("passed", "skipped", "failed")
    }
}