Search code examples
gradlejunitjunit5junit-jupiter

JUnit 5 Gradle: Do test classes really have to be suffixed with "Test(s)"?


It appears as if JUnit 5 tests aren't found if the file- and class name does not end with "Test" or "Tests" when using Gradle. Is it possible to configure Gradle to treat everything in the test directory as test classes?


Solution

  • From this doc, note that there is a plugin called 'junit-platform-gradle-plugin'. The salient part is the following:

    junitPlatform {
        filters {
            includeClassNamePattern '.*'
        }
    }
    

    (as opposed to '.*TestCase')

    Here is the full build.gradle file (and an example that runs tests named ExampleTestCase and Example2):

    apply plugin: 'java'
    
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
        }
    }
    
    apply plugin: 'org.junit.platform.gradle.plugin'
    
    repositories {
        jcenter()
    }
    
    ext.junitJupiterVersion  = '5.0.2'
    
    dependencies {
        testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
        testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
    }
    
    junitPlatform {
        filters {
            includeClassNamePattern '.*'
        }
    }