javagradlejunit5source-sets

Gradle can't find tests in additional source set


My goal is to set up a source set for integration tests. I created a source set called "intTest". I put a simple test inside:

package com.github.frozensync;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

class Application {

    @Test
    @DisplayName("should work")
    void foo() {
        assertEquals(2, 2);
    }
}

When I try to run it, I get the following error:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.github.frozensync.Application](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0ms

This is my project structure:
project structure

And this is my build.gradle

plugins {
    id 'java'
}

group 'com.github.frozensync'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    intTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

idea {
    module {
        testSourceDirs += sourceSets.intTest.java.srcDirs
        testResourceDirs += sourceSets.intTest.resources.srcDirs
    }
}

configurations {
    intTestImplementation.extendsFrom implementation
    intTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
    intTestImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

test {
    useJUnitPlatform()
}

task integrationTest(type: Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.intTest.output.classesDirs
    classpath = sourceSets.intTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest

I've followed a guide by Gradle. When I faced the issue, I tried the following:
- Run ./gradlew cleanIntegrationTest integrationTest to bypass IntelliJ but it still ran 0 tests
- Add the idea Gradle plugin.
- Add dependsOn from this.
- Solutions from this.

How can I enable Gradle to discover tests inside the source set "intTest"?


Solution

  • Your test is a JUnit 5 test, but you haven't told Gradle. Just like for the test task you need to call

    useJUnitPlatform()
    

    in the configuration of your integrationTest task.