A.3. Master example for Maven, Ant, and Gradle

Your first stop regarding Spock installation should be the Spock-example GitHub repository (https://github.com/spockframework/spock-example). This full project with Spock tests contains build files for Gradle, Maven, and Ant (depending on your build system, you may not need all of them). All my instructions for the next sections are extracted from this project.

All code listings in this appendix are segments of the pom.xml file located in that repository. I summarize the instructions for Maven and Gradle.[1] I assume that you already have a Java project up and running and you want to add Spock tests.

1

Ant isn’t a build system. It’s a relic of the past and should die in flames. If you’re starting a new Java project with Ant, please bang your head against the wall now.

A.3.1. Spock with Maven

If you use Maven, add the code in the following listing to your pom.xml file in the Build > Plugins section.

Listing A.1. Adding Groovy support in Maven
<plugin>
       <groupId>org.codehaus.gmavenplus</groupId>
       <artifactId>gmavenplus-plugin</artifactId>
    <version>1.4</version>
    <executions>
    <execution>
       <goals>
               <goal>compile</goal>
               <goal>testCompile</goal>
       </goals>
    </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
       <useFile>false</useFile>
       <includes>
               <include>**/*Spec.java</include>
               <include>**/*Test.java</include>
       </includes>
    </configuration>
</plugin>

This sets up Groovy support in Maven and Surefire. In pom.xml, in the dependency section, add the code shown in the next listing.

Listing A.2. Adding Spock dependencies in Maven
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.0-groovy-2.4</version>
    <scope>test</scope>
</dependency>
<dependency> <!-- enables mocking of classes
                       (in addition to interfaces) -->
    <groupId>cglib</groupId>
    <artifactId>cglib-nodep</artifactId>
    <version>3.1</version>
    <scope>test</scope>
</dependency>
<dependency> <!-- enables mocking of classes without default
            constructor (together with CGLIB) -->
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>2.1</version>
    <scope>test</scope>
</dependency>

That’s it. Now you can build Spock tests via Maven. Running mvn test from the same directory where the pom file exists should correctly detect and run your Spock tests (assuming they all have names that end in *Spec, as I show in the book).

A.3.2. Spock with Gradle

If you use Gradle in your Java project, things are even simpler. Groovy compilation is already taken care of, and you only need the Spock dependencies in your build.gradle file, as shown in the next listing.

Listing A.3. Gradle settings for Spock
dependencies {

    [....other dependencies here...]
// mandatory dependencies for using Spock
  compile "org.codehaus.groovy:groovy-all:2.4.1"
  testCompile "org.spockframework:spock-core:1.0-groovy-2.4"

// optional dependencies for using Spock
  testRuntime "cglib:cglib-nodep:3.1"          // allows mocking of classes
 (in addition to interfaces)
  testRuntime "org.objenesis:objenesis:2.1"    // allows mocking of classes
           without default constructor (together with CGLIB)
}

In addition, make sure that you already use Maven Central, as the following listing shows.

Listing A.4. Gradle settings for Spock repository
repositories {
  // Spock releases are available from Maven Central
  mavenCentral()
}

Now you’re ready to use Spock from the command line. Running gradle test from the same directory that holds the build.gradle file will run all Spock tests.

A.3.3. Spock in an enterprise environment

If you want to use Spock inside a company that has a binary repository like Nexus (www.sonatype.org/nexus/) or Artifactory (www.jfrog.com/open-source/), you should consult their documentation on how to use them as a proxy for Maven Central. Talk with the administrator of these repositories for guidance on company policies regarding external library usage.

Let’s see how IDEs handle Spock support.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.123