S-JIS[2017-09-14] 変更履歴
JUnit5をGradleで使用する方法。
|
JUnit5(Jupiter)をGradleで扱う場合はJUnitのGradleプラグインを使用する。
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' } } repositories { mavenCentral() } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.junit.platform.gradle.plugin' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 dependencies { testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0") // testCompile("org.junit.jupiter:junit-jupiter-params:5.0.0") // parameterized tests testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0") testRuntime("org.junit.platform:junit-platform-launcher:1.0.0") // for IDE } task wrapper(type: Wrapper) { gradleVersion '4.1' jarFile file('.buildtools/gradlew.jar') }
テストを記述するにはdependenciesにjunit-jupiter-apiが必要。
テストを実行する為にdependenciesにjunit-jupiter-engineが必要。
gradleコマンドでテストを実行する為にはJUnitのGradleプラグインjunit-platform-gradle-plugin(org.junit.platform.gradle.plugin)が必要。
gradleコマンドでEclipse用のプロジェクトを作り、Eclipseでテストを実行する場合はdependenciesにjunit-platform-launcherが必要。
(JUnit5対応版Eclipseを直接使う場合は不要)
build.gradleにjunitPlatformというブロックを作り、JUnit5の設定を記述することが出来る。
(gradleコマンドでテストを実行する際に使用される)
〜 junitPlatform { filters { includeClassNamePatterns '.*Test', '.*Tests', '.*Example' tags { // include 'tag1' // exclude 'tag1' } } configurationParameter 'junit.jupiter.testinstance.lifecycle.default', 'per_method' } dependencies { 〜 }
configurationParameterを複数書きたい場合はconfigurationParametersを使用する。
テストの実行方法は普通のGradleと同じ。
> gradlew clean test 〜 Test run finished after 141 ms [ 12 containers found ] [ 0 containers skipped ] [ 12 containers started ] [ 0 containers aborted ] [ 12 containers successful ] [ 0 containers failed ] [ 24 tests found ] [ 1 tests skipped ] [ 23 tests started ] [ 0 tests aborted ] [ 23 tests successful ] [ 0 tests failed ]
ただし、通常のGradleのテストで作られるレポートは作られない。
デフォルトでは、テスト対象クラスはクラス名がTestまたはTestsで終わるクラスのみ。
junitPlatformのfiltersのincludeClassNamePatternsで、テスト対象クラス名のパターンを指定することが出来る。
junitPlatform { filters { includeClassNamePatterns '.*Test', '.*Tests', '.*Example' } }
なお、JUnitのGradleプラグインをbuild.gradleに入れていない場合、テストの実行コマンドは成功するが、実質的にはテストは行われない。
> gradlew clean test
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
> dir build
2017/09/14 21:08 <DIR> .
2017/09/14 21:08 <DIR> ..
2017/09/14 21:08 <DIR> classes
2017/09/14 21:08 <DIR> reports
2017/09/14 21:08 <DIR> resources
2017/09/14 21:08 <DIR> test-results
2017/09/14 21:08 <DIR> tmp
JUnit5のテストがちゃんと実行された場合はreportsディレクトリーは作られない。
テストは従来のJUnit3,4で記述し、テストの実行だけJunit5(Vintage)で行う場合もJUnitのGradleプラグインを使用する。
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.junit.platform.gradle.plugin' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile("junit:junit:4.12") testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0") testRuntime("org.junit.platform:junit-platform-launcher:1.0.0") // for IDE } task wrapper(type: Wrapper) { gradleVersion '4.1' jarFile file('.buildtools/gradlew.jar') }
↓実行例
> gradlew clean test 〜 Test run finished after 57 ms [ 3 containers found ] [ 0 containers skipped ] [ 3 containers started ] [ 0 containers aborted ] [ 3 containers successful ] [ 0 containers failed ] [ 4 tests found ] [ 1 tests skipped ] [ 3 tests started ] [ 0 tests aborted ] [ 3 tests successful ] [ 0 tests failed ]
「gradlew cleanEclipse eclipse」でEclipseプロジェクトの設定を生成してインポートし、Eclipseの機能でテストを実行することが出来る。
ただし、build.gradleにjunit-platform-launcherが入っていないと、テストの実行時に以下のような例外が発生する。
java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:368) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:363) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:307) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:222) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 11 more