GradleとJUnitの例

GradleとJUnitの例

Gradleでは、次のようにJUnit依存関係を宣言できます。

build.gradle

    apply plugin: 'java'

    dependencies {
        testCompile 'junit:junit:4.12'
    }

デフォルトでは、JUnitにはhamcrest-coreのバンドルコピーが付属しています

$ gradle dependencies --configuration testCompile

testCompile - Compile classpath for source set 'test'.
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

1. Gradle + JUnit + Hamcrest

通常、便利なhamcrest-libraryライブラリが必要なので、hamcrest-coreのJUnitバンドルコピーを除外し、元のhamcrest-coreライブラリを含める方がよいでしょう。 更新されたpom.xmlを再度確認します。

build.gradle

    apply plugin: 'java'

    dependencies {
        testCompile('junit:junit:4.12'){
            exclude group: 'org.hamcrest'
        }
        testCompile 'org.hamcrest:hamcrest-library:1.3'
    }

依存関係を再度確認します。

$ gradle dependencies --configuration testCompile

testCompile - Compile classpath for source set 'test'.
+--- junit:junit:4.12
\--- org.hamcrest:hamcrest-library:1.3
     \--- org.hamcrest:hamcrest-core:1.3