Spock Framework – I – InProgress

Spock aims to be a more powerful alternative to the traditional JUnit stack, by leveraging Groovy features.

Groovy is a JVM-based language which seamlessly integrates with Java

At a glance Spock framework is ,

  • BDD style testing and specification framework
  • Which run Junit under the hood
  • For Java and Groovy applications
  • Test case writing in groovy
  • All three component combine with single library
    • Unit testing
    • Mocking
    • BDD
All three component combine with single library

Spock Features Highlights

  • Highly Expressive
  • Spock enforce a clear test structure
  • BDD style test can be expressive through the “given, when and then” blocks
  • Compatible with all major build tools and continuous integration providers
  • Inbuilt Mocking and Stubbing
  • Parameterized test and Data driven test ( Data table based and SQL table based )

BDD Style test blocks

  • Given – any setup needed before a test is run.
  • When – This is where we provide a stimulus to what is under test. In other words, where we invoke our method under test
  • Then – This is where the assertions belong. In Spock, these are evaluated as plain Boolean assertions
  • Expect – This is a way of performing our stimulus and assertion within the same block.
  • Cleanup – Here we tear down any test dependency resources which would otherwise be left behind.


•A specification,

•is represented as a Groovy class that extends from “spock.lang.Specification”.

•The name of a specification usually relates to the system or system operation described by the specification.

•EX : Unicorn Event Routing

•Fixture Methods

•def setupSpec() {}    // runs once –  before the first feature method

•def setup() {}        // runs before every feature method

•def cleanup() {}      // runs after every feature method

•def cleanupSpec() {}  // runs once –  after the last feature method

•Blocks :

        given, when, then, expect, cleanup, and where

More features

1.Expecting Exceptions

2.Data Pipes

3.Data base Driven

4.Data Tables

5.Test Failing Visualization



8.Helper Methods



11.Specifications as Documentation

•Use case : Xnicorn XRT Event Routing

•XNICORN Consume messages from XTS and XICOS

•Based on the Message content and Xounterparty data

•Messages are routing from



•To multiple Targets






Spock With Spring Framework

•Similar to Junit spring testing

•Its support all kind of spring test annotations






•DetachedMockFactory – allow the creation of Spock mocks outside of a specification

•@SpringBean – Similar to spring @MockBean

•@SpringSpy – If you want to spy on an existing bean

Leave a Reply

Your email address will not be published. Required fields are marked *