LoginSignup
7
6

More than 5 years have passed since last update.

Spockを使ってみる (導入編)

Posted at

Spockを使ってみたので、忘れないうちにメモ

試した環境

  • NetBeans 8.0.2
  • JDK8
  • Maven or Gradle

NetBeansで "SpockTest" projectを作る

NetBeansで、File... -> New Project... -> Maven -> Java Application

  • Project name
    • "SpockTest"

pom.xmlを書き換える

Projects/SpockTest/Project Files/pom.xml

  • <dependencies>
  • <build>
  • <repositories>
  • <pluginRepositories>

にSpock用の記述を追加

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hoge</groupId>
    <artifactId>SpockTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spock (must) -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.0-groovy-2.3-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

        <!-- Spock (optional) -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spock (must) -->
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <providerSelection>2.0</providerSelection>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.gmaven.runtime</groupId>
                        <artifactId>gmaven-runtime-2.0</artifactId>
                        <version>1.4</version>
                        <exclusions>
                            <exclusion>
                                <groupId>org.codehaus.groovy</groupId>
                                <artifactId>groovy-all</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.3.8</version>
                    </dependency>
                </dependencies>
            </plugin>

            <!-- Spock (optional) -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Spec.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Spock (optional) -->
    <repositories>
        <repository>
            <id>spock-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!-- Spock (optional) -->
    <pluginRepositories>
        <pluginRepository>
            <id>spock-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

(option) Gradleの場合

ちゃんと試してないけど、多分こう

apply plugin: "groovy"

def javaVersion = 1.7
def defaultEncoding = 'UTF-8'

// Spock works with Java 1.5 and above
sourceCompatibility = javaVersion
targetCompatibility = javaVersion

repositories {
  mavenCentral()

  // Spock snapshots are available from the Sonatype OSS snapshot repository
  maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
}

dependencies {
  // mandatory dependencies for using Spock
  compile "org.codehaus.groovy:groovy-all:2.3.8"
  testCompile "org.spockframework:spock-core:1.0-groovy-2.3-SNAPSHOT"

  // optional dependencies for using Spock
  testCompile "org.hamcrest:hamcrest-core:1.3"
  testRuntime "cglib:cglib-nodep:3.1"
  testRuntime "org.objenesis:objenesis:2.1"
}

compileJava {
  options.encoding = defaultEncoding
}

task wrapper(type: Wrapper) {
  gradleVersion = "2.2"
}

Spock(groovy)用のテストディレクトリを作る

(project root)/src/test/groovy/com/hoge/spocktestなディレクトリ構造を作る。
(project root)/src/main/javaからコピペすると楽。

↑の通り作ると、NetBeansがProjects view (左の一覧) で"Groovy Test Packages"と表示してくれる。

テスト対象クラス (お試し) を作る

Projects/SpockTest/Source Packagescom.hoge.spocktestパッケージを右クリックして、以下の新しいJava Class "Calc.java" を追加する。

Calc.java
package com.hoge.spocktest;

public class Calc {

    public int add(int lhs, int rhs) {
        return lhs + rhs;
    }
}

テストコード (Spock) を書く

Projects/SpockTest/Groovy Test Packagescom.hoge.spocktestパッケージを右クリックして、以下の新しいGroovy Class "CalcSpec.groovy" を追加する。

クラスを追加する際に、"Java class..." ではなく "Groovy class..." を探して使います

CalcSpec.groovy
package com.hoge.spocktest;

import spock.lang.*;
import com.hoge.spocktest.*;

class CalcSpec extends Specification {

    private Calc instance;

    def setup() {
        instance = new Calc()
    }

    def "adding two numbers"() {
        expect:
        instance.add(a, b) == c

        where:
        a | b | c
        1 | 3 | 4
        7 | 4 | 11
        0 | 0 | 0
    }
}

ビルド&テスト実行する

NetBeansメニューから, Run -> Clean and Build Project.

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6