My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Run your first Spock specification.
Featured
Updated Mar 25, 2012 by pnied...@gmail.com

Spock Web Console

The easiest way to get to know Spock is http://webconsole.spockframework.org, which instantly lets you view, edit, run and even publish Spock specifications for others to see. Zero install required!

Spock Example Project

If you prefer to run Spock on your machine, just download spock-example-0.6.zip (or clone the Git repository) and type ./gradlew test (Windows: gradlew test). Seconds later, you will be running your first Spock specifications. The only prerequisite is that you have JDK 5 or higher installed; everything else will be downloaded automatically. Also included are build scripts for Ant and Maven, and instructions how to generate an Eclipse or IDEA project. For more information, see the README file.

Need help? Ask in our forum!


The remainder of this page explains in detail how to manually setup Spock in various environments.

Command line

  • Download and setup Java 5 or higher (JRE will do for our purposes)
  • Download and setup Groovy 1.8.6 binary release (download page)

Detailed instructions for these two steps can be found in the Groovy tutorial.

  • Download junit-4.9.jar (download page)
  • Download spock-core-0.6-groovy-1.8.jar
  • Create a file named HelloSpock.groovy with the following content: HelloSpock.groovy
  • Run HelloSpock with groovy -cp "junit-4.9.jar:spock-core-0.6.jar" HelloSpock (make sure the classpath is correct; on Windows, separate classpath entries with ; instead of :)

Ant

Verified against Ant 1.8

  • Create file build.xml with the following content: build.xml
  • Create file src/HelloSpock.groovy with the following content: HelloSpock.groovy
  • Run HelloSpock with ant test

Gradle

Verified against Gradle 1.0-milestone-9

  • Create file build.gradle with the following content: build.gradle
  • Create file src/test/groovy/HelloSpock.groovy with the following content: HelloSpock.groovy
  • Run HelloSpock with gradle test

Maven

Verified against Maven 2.1

  • Create file pom.xml with the following content: pom.xml
  • Create file src/test/groovy/HelloSpock.groovy with the following content: HelloSpock.groovy
  • Run HelloSpock with mvn test

Eclipse

Verified against Eclipse 3.6, 3.7

  • Install the Groovy Eclipse Plugin
  • Important: Make sure to use the latest release or snapshot version of the Groovy Eclipse plugin.
  • Enable the following option: Preferences->Groovy->Use monospace font for JUnit. This is important for Spock's condition output to be aligned correctly. If this doesn't work immediately, uncheck the option, press 'Apply', and check the option again.
  • For Eclipse 3.5 and below, make sure you have a source attachment set for the spock-core Jar. (Otherwise you might run into the following Eclipse JDT bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=175582) For Maven projects, the easiest way to achieve this is mvn eclipse:eclipse -DdownloadSources.
  • Create a new Java project named HelloSpock
  • Download spock-core-0.6-groovy-1.8.jar
  • Add JUnit 4 (4.7 or higher) and spock-core-0.6-groovy-1.8.jar to the project's class path
  • In the src folder, create a Groovy class named HelloSpock with the following content: HelloSpock.groovy
  • In Project Explorer, right-click HelloSpock and select Run As -> JUnit Test

Here is a screenshot of Spock in action:

IntelliJ IDEA

Verified against IDEA 10, 11

  • Download and extract Groovy 1.8.6 binary release (download page)
  • Download junit-4.9.jar (download page)
  • Download spock-core-0.6-groovy-1.8.jar
  • Create a new IDEA project named HelloSpock and add a Java module.
  • In "Module settings", add a "Single-Entry Module Library" for groovy-all-1.8.6.jar, junit-4.9.jar, and spock-core-0.6.jar
  • In the src folder, create a Groovy class named HelloSpock with the following content: HelloSpock.groovy
  • In the "Project" window, right-click HelloSpock and select "Run"

Netbeans

As of Netbeans 6.7.1/6.8M1, the NetBeans Groovy plugin does not support running JUnit 4 tests, and therefore Spock specifications. For general information about the NetBeans Groovy plugin, go to http://groovy.codehaus.org/NetBeans+Plugin.

Comment by christia...@gmail.com, Apr 14, 2009

By using the current development version of the Groovy 1.6 plugin for Eclipse the execution of Spock specifications within Eclipse works. The plugin is available here:

http://dist.groovy.codehaus.org/distributions/updateDev_1.6/
All you have to do is to create a "Run Configuration" for the JUnit4 test runner. In my case Eclipse did not find the test class, so I had to type it manually. But then running the tests worked just fine.

Chris H.

Comment by project member pnied...@gmail.com, Apr 14, 2009

Hi Chris,

thanks for the hint! I'll give it a try and update the documentation.

Cheers, Peter

Comment by jerome.l...@gmail.com, Jun 14, 2010

For Intellij users, you might need to change the Compiler settings to make sure groovy files are compiled. Otherwise, Intellij won't find the class when trying to run them. (add !?.groovy to Compiler settings)

(Tested using both maven 2.2.x and intellij 9.0.2 (IU-95.66) and spock 0.4 - groovy 1.7)

Comment by richfree...@gmail.com, Mar 24, 2011

For Maven 3, you need to remove the spock plugin from pom.xml, and name the test class according to the surefire conventions, e.g. HelloSpockTest?.groovy. Don't forget to also change the name of the class in the groovy source.

(Tested with maven 3.02, groovy 1.75, JVM 1.6.0_24 on OS X 10.6.7)

Comment by firehoo...@gmail.com, Mar 31, 2011

Anyone using m2eclipse that could post how-to?

Comment by josef.be...@gmail.com, Dec 17, 2011

For Eclipse use the instructions are not clear. Well, at least to me. The Eclipse Groovy plug-in does not have everything you need. There is no Spock jar included, for example. And, getting the required jar file is not straight forward. The download links just provide a spock project download which indirectly get you to the jar file, and the resulting jar file is no good since Spock won't run with Groovy 1.8, you have to search for yet another version of the jar. Then, Eclipse won't do a "Run as ..." for the HelloSpock.groovy source. Oy veah!

Finally got the hello world to run. I can't even contemplate inflicting this on my fellow team mates.

-- Josef

Comment by project member pnied...@gmail.com, Dec 17, 2011

I hope your team doesn't download Jars and create IDE projects from hand. spock-example contains both a Maven and Gradle build from which a fully functioning IDE project can be created.

I think it's no big surprise that spock-core-groovy-1.7 won't run with Groovy 1.8. For Groovy 1.8 you'll have to use spock-core-groovy-1.8-SNAPSHOT.

Comment by jdbeu...@gmail.com, Feb 5, 2012

I tried running the following with 'groovy HelloSpock', but got an error about not finding any groovy-all 1.8.0-beta-3-SNAPSHOT. I guess that snapshot is gone now, although I still see 1.8.0-beta-3 and even 1.8.5 now in the repo. The snapshot dependency came from this artefact; can it be updated? (To work around this, I edited my ~/.groovy/grapes/org.spockframework/spock-core/ivy-0.5-groovy-1.8.xml to have version 1.8.5.)

import spock.lang.Specification

@Grapes([
    @Grab(group='org.spockframework', module='spock-core', version='0.5-groovy-1.8'),
    @Grab(group='junit', module='junit', version='4.7')
])
class HelloSpock extends Specification {
    def "length of Spock's and his friends' names"() {
        expect:
        name.size() == length

        where:
        name        | length
        "Spock"     | 5
        "Kirk"      | 4
        "Scotty"    | 6
    }
}
Comment by jdbeu...@gmail.com, Feb 5, 2012

I just found the following thread, which suggests that Spock 0.5 is deprecated for Groovy 1.8 and that I need Spock 0.6. But, 0.6 is not released yet? Where/when can I @Grab it?

http://spock-framework.3207229.n2.nabble.com/Problem-with-Unroll-with-groovy-1-8-td6317496.html

Comment by project member pnied...@gmail.com, Feb 6, 2012

0.5-groovy-1.8 was based on a pre-release version of Groovy 1.8. Until the 0.6 release, you should use 0.6-groovy-1.8-SNAPSHOT (or a timestamped version thereof), which you can get from http://m2repo.spockframework.org/snapshots.


Sign in to add a comment
Powered by Google Project Hosting