Get in touch

Awesome Image Awesome Image

Software Development March 24, 2023

Spock-Framework: The Logical Choice for Modern Testing

Written by Dharmesh Patel



We’ll look at Spock, a Groovy testing framework. Mainly, Spock aims to be a more powerful alternative to the traditional JUnit stack, by leveraging Groovy features.

By making use of Groovy, Spock introduces new and expressive ways of testing our Java applications, which simply aren’t possible in ordinary Java code. We’ll explore some of Spock’s high-level concepts during this article, with some practical step-by-step examples.


You have some knowledge about spring-boot and testing concept like mocking, Stubbing, etc.

Here we learn the Spock framework with the Spring boot application.

To use Spock in spring boot, add the below dependency into pom.xml 


And add a plugin for groovy support 


Simple test using Spock framework

Make groovy class and extend the specification to it (spock.lang.specification).

For testing we made one simple method.

class Test1 extends Specification {
    def "Simple Test"(){
            1+1 == 2

Here “def” keyword is used to define method after this you can add method discerption.

Now run the test


You see Test Case Passed successfully.

Simple Test

Controller Test-Cases

First, create a ‘Hello’ controller

public class Hello {
public String greeting (){
    return "Hello World! ";
Now for this controller, we are writing test cases using Spock.
class HelloTest extends Specification {
   def cont = new Hello ()
   MockMvc mvc = MockMvcBuilders.standaloneSetup(cont). build()
   def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
       expect: "Status is 200 and the response is 'Hello world!'"
               .contentAsString == "Hello World! "

Run server


  1. Expect: This is a way of performing our stimulus and assertion within the same block. Depending on what we find more expressive, we may or may not choose to use this block
  2. When – This is where we provide a stimulus to what is under test. In other words, where we invoke our method under test
  3. Then – This is where the assertions belong. In Spock, these are evaluated as plain Boolean assertions, which will be covered later

Important Tips

1) All the blocks in a Spock based spec are optional. However, if present “when” and “then” should appear together in sequence.

2) If you just need to validate an assertion (and you don’t have a need of when block), you can use “expect” block. It can be used to assert pre-conditions even before when and then blocks (and can be used multiple times in a test).

Important Functions

Here We Have 4 Methods used to define global or redundant code.

* Call only one time for all test cases
* You can define a spec for static or whole test cases that have the same value
* */
def setupSpec() {
* Call only once at the end of all test cases
* */
def cleanupSpec() {
* Call for every test case
* It calls for every time before the test case
* */
def setup () {
* Call for every test case
* It calls for every time after test case
* */
def cleanup() {

Generate Reports:

Using Spock, you can generate reports too
For that, we must add 3 dependencies into pom.xml




After that reload the maven file and run all the test cases 

Then build -> spock-reports -> index.html you can find reports for test cases you run.

(For Reference you see the below image.) 

Built Spock Report

Below Screenshot Showing Index.html 

specification run Result

Mocking and Stubbing


For mocking you must use the Mock () method  

Ex: –    def studentDatabase = Mock (StudentDatabase.class)

With Mocks, you don’t have to do a lot of setups, but you can validate the interactions that happened with the mock objects supplied to the application under test.

With mocks, you can do things like

  • What arguments the mocks were called?
  • What was the total count of invocations etc?
  • Ascertaining the order of mocks.

For a mock example refer to the GitHub link.

Link = Spock Demo Document


Stubbing is nothing but setting up pre-defined or canned responses on the Mock invocations to test the different flows/scenarios of the application under test.

A Stub is like a Mock which in a way emulates the behaviour of the real object. You can simply call it a programmed Mock.


StubbedObject.StubbedMethod(argument list) >> “Stubbed Response”

Here we are using @WebMvcTest annotation and @MockMvc for calling the rest API.

In a later version of 3.0 spring boot, you must mention your main runner class (@springbootapplication annotation class) into @ContextConfiguration (see below stubbing example)

Here in the example, we use @SpringBean for adding bean.

SpringBean Annotation

Registers mock/stub/spy as a spring bean in the test context.

 You also need to directly assign the Mock/Stub/Spy to the field using the standard Spock syntax. You can even use the initializer blocks to define common behaviour however, they are only picked up once they are attached to the Specification.

@SpringBean definitions can replace existing Beans in your ApplicationContext.

To run the below example, we must add one more dependency


Stubbing Example: 

@ContextConfiguration (classes = Demo1Application)
class HelloTest extends Specification {
   protected MockMvc mvc
   HelloService helloService = Stub() {
       greeting () >> "Stubbed data"
   def "when get is performed then the response has status 200 
and service method calls"() {
       when: "Calls Method"
       def result = 
       then: "check value"
       and: "check for stubbed string"
.contentAsString == "Stubbed data"


in this blog, we created test cases for the controller. the same way you create test cases for service or any class. If you prefer readable test cases, then I’m recommended you use the Spock framework for writing test cases.

To see the complete demo, use the GitHub link (Spock Demo Document).


Bringing Software Development Expertise to Every
Corner of the World

United States



United Kingdom




New Zealand










South Africa