前提
- Spock 0.7
- Groovy 2.0.7
- 動作確認環境
- Eclipse 4.3 SR1
- Eclipse Plugin
- Groovy Eclipse
- Gradle IDE
手順
Eclipseで新規プロジェクト(一般プロジェクト)を作成し、プロジェクト直下に、以下のbuild.gradleを配置する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
apply plugin: 'groovy' | |
apply plugin: 'eclipse' | |
repositories { mavenCentral() } | |
dependencies { | |
compile "org.codehaus.groovy:groovy-all:2.0.7" | |
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0' | |
} | |
task mkdirs << { | |
sourceSets.all { | |
it.groovy.srcDirs.each { it.mkdirs() } | |
it.resources.srcDirs.each { it.mkdirs() } | |
} | |
} |
コマンドラインでプロジェクトのディレクトリに移動し、以下のタスクを実行する。
gradle mkdirsプロジェクトを右クリックし、「Refresh」を選択すると、プロジェクトがGroovyプロジェクトとして認識される。再度プロジェクトを右クリックし、「Configure」-「Convert to Gradle Project」を選択すると、Gladleプロジェクトに変換される。
gradle cleanEclipse
gradle eclipse
この時点で、以下の設定も確認しておく。
- 「Windows」-「Preferences」-「Groovy」
- Use monospace font for JUnit (deprecated)にチェック
- これにより、JUnit実行時に monospace fontが使用される
- 「Windows」-「Preferences」-「Gradle」
- Disable underlininig for .gradle files
- これにより、.gradleファイルを開いた際に、余計な下線が表示されるのを抑止
以下のSpockのテストケース(SampleSpec.groovy)を、「src/test/groovy」に作成する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class SampleSpec extends Specification { | |
def "test sample"() { | |
expect: | |
def value = 1 | |
value == 2 | |
} | |
} |
配置したSampleSpec.groovyを右クリックし、 「Run as」-「JUnit Test」を選択するとテストが実行される、テストが失敗する(このテストは意図的に失敗するよう書かれている)。
以下のように修正し、再度テストを実行すると、テストが成功することを確認できる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class SampleSpec extends Specification { | |
def "test sample"() { | |
expect: | |
def value = 2 | |
value == 2 | |
} | |
} |
0 件のコメント:
コメントを投稿