mike-neckのブログ

JavaかJavaFXかJavaEE(なんかJava8が多め)

How to run test class whose name doesn't end with Test in JUnit5 with Gradle.

JUnit5 publishes junit-platform-gradle-plugin. With it you can run JUnit5 tests with gradle. But if you don't give Test at the end of the test class name, this plugin ignores to run it. If you want to run a test classes whose name don't end with Test, ex. FooSpec, BarTestCase, you have to specify a name pattern of them via includeClassName(or includeClassNames) in junitPlatform.filters Closure.

example
buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3"
  }
}

apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
  mavenCentral()
  jcenter()
}

dependencies {
  testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M3"
  testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M3"
}

// specify your test class names here
junitPlatform {
  filters {
    includeClassNamePattern '^.*TestCase$'
    includeClassNamePatterns '^.*Spec$', '^.*Tests?$' 
  }
}

With this configuration, you can run tests whose name ends with Test, Tests, Spec, TestCase.