Search
  • Money Tips | Product Reviews

Spock Framework - Beginner's Guide

Updated: Jul 25

If you are an experienced Spock user or a beginner, you will find some good information about the Spock framework. Read and learn all the basics and the guide to how Spock can be used to improve your productivity be it for any type of testing - unit, integration or end-to-end.

Spock Framework

Whats is Spock Framework?

Spock framework in simple terms provides a BDD framework for writing tests so you can validate your application's functionality. Spock is suited for Java and Groovy applications and it is very easy to understand and use for your projects.


Why Spock framework?

Spock offers an easy-to-use specification language that is built on top of JUnit. In fact, Spock is inspired from JUnit, RSpec, jMock, Mockito, Groovy, Scala, Vulcans, and others. This means Spock is supported by IDEs and can be used just for any test, be it unit or integration testing. Little to no learning curve and you will use Spock with fun since it is very intuitive and simplifies most testing setup and scenarios.

Spock enables developers and developers-in-test to write tests quickly and effortlessly in a short time. Thus, the essential testing of your application is now made easier with this Spock tool.


How to install and use the Spock framework?

Simply add this one dependency to your Java or Gradle project. Shown here is for Gradle project build.gradle. Spock download for your project can be done by adding this dependency.


testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'

For Maven:

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.3-groovy-2.5</version>
    <scope>test</scope>
</dependency>

For other build tools, check out this link to get the dependency syntax.


Spock is written in Groovy language and for Java developers, Groovy is easy to learn since it is a superset of Java with easier syntax aimed at reducing the boilerplate code in Java. So let's leave behind the old Junit way of writing tests and embrace the more fun Spock framework for writing the same Junit tests in a much elegant and nicer way.


In the Spock world, tests are called Specs. For example, a Spock test named MyFirstSpec instead of MyFirstTest as in the Junit world. Maven needs to be told to treat files ending in Specs as tests. So, add the *Spec.groovy or *Spec.java to your pom.xml like this in your Maven surefire plugin XML configuration.


<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>x2.22.2</version>
        <configuration>
          <includes>
            <include>**/*Spec.java</include>
            <include>**/*Test.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Let's see how to write a JUnit test in Spock. A simple Junit test might look like this.

import org.junit.Test;
import static org.junit.Assert.*;

public class MyTest {

    @Test
    public void testConcatenate() {
        MyClass myClass = new MyClass();

        String resultString = myClass.concatenate("hello", "world");

        assertEquals("heloworld", resultString );

    }
}

To write this same test in Spock, create a groovy class file name MySpec.groovy.


import spock.lang.Specification

class MySpec extends Specification {

def "test cocatinate  method"() {
    given:
        def myClass = new MyClass();
    when: "I pass two strings to concatinate method"
        String resultString = myClass.concatenate("hello", "world");        
    then: "the concatination work properly"
       "heloworld" == resultString
   }
  
}

Every Spec has to extend Specification. Looking at the above, you can infer that:

  1. Spock gives you the BDD style given when then blocks for easy test flow

  2. every given when then can have details of what they do. This allows for well-documented test and living documentation that every developer can update while making changes to the Spec in the future.

  3. The simplicity of Groovy syntax removes all that verbose, unnecessary annotations, etc.. making Spock test easier to write and maintain.

Make reports look better with Spock-reports

Add the stock reports dependency to your build.gradle or pom.xml and get a nice report after running your Spock tests!


testImplementation group: 'com.athaydes', name: 'spock-reports', version: '1.2.10'


How to generate Spock reports?

Once you have added the Spock reports dependency, simply run your tests. After the test is run, you will see a "spock-reports" folder. In that folder, open the index.html file to see the results summary! For information about customizing your spock-reports, click here.

Look at the nice view of spock-reports showing a clear picture of what went wrong especially needed when investigating a test failure.


Where can I find more information about Spock?

You can check out the well-documented sites for reference spock documentation here and the Javadoc here.


How can I get involved and contribute?

Get the source code here and then once you find any issues, you can always report them here in their issue tracker. They also have a discussion Chat in Gitter.


Most popular:

Gradle Dependencies - What is it all about?

Selenium Vs Cypress: Which is the better automation tool?

Selenium 4 Java and Python - Chrome DevTools to capture XHR response body

How To Record Using JMeter Chrome Extension

Selenium 4 Java and Python - Chrome DevTools to capture XHR response body


Click here to check out more posts like these.

Recommended