Guice Module

Integration with the Guice IoC container. For examples see the specs in the codebase.

Spring Module

The Spring module enables integration with Spring TestContext Framework. It supports the following spring annotations @ContextConfiguration and @ContextHierarchy. Furthermore, it supports the meta-annotation @BootstrapWith and so any annotation that is annotated with @BootstrapWith will also work, such as @SpringBootTest, @WebMvcTest.


Spock 1.1 introduced the DetachedMockFactory and the SpockMockFactoryBean which allow the creation of Spock mocks outside of a specification.


Although the mocks can be created outside of a specification, they only work inside the scope of a specification. So don’t perform any actions on them until they are attached to one.

Java Config

public class DetachedJavaConfig {
  def mockFactory = new DetachedMockFactory()

  GreeterService serviceMock() {
    return mockFactory.Mock(GreeterService)

  GreeterService serviceStub() {
    return mockFactory.Stub(GreeterService)

  GreeterService serviceSpy() {
    return mockFactory.Spy(GreeterServiceImpl)

  FactoryBean<GreeterService> alternativeMock() {
    return new SpockMockFactoryBean(GreeterService)


Spock has spring namespace support, so if you declare the spock namespace with xmlns:spock="" you get access to the convenience functions for creating mocks.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

  <spock:mock id="serviceMock" class=""/>   (1)
  <spock:stub id="serviceStub" class=""/>   (2)
  <spock:spy id="serviceSpy" class=""/> (3)

  <bean id="alternativeMock" class="org.spockframework.spring.xml.SpockMockFactoryBean"> (4)
    <constructor-arg value=""/>
    <property name="mockNature" value="MOCK"/>                                           (5)
  1. Creates a Mock

  2. Creates a Stub

  3. Creates a Spy

  4. If you don’t want to use the special namespace support you can create the beans via the SpockMockFactoryBean

  5. The mockNature can be MOCK, STUB, or SPY and defaults to MOCK if not declared.


To use the mocks just inject them like any other bean and configure them as usual.

@Autowired @Named('serviceMock')
GreeterService serviceMock

@Autowired @Named('serviceStub')
GreeterService serviceStub

@Autowired @Named('serviceSpy')
GreeterService serviceSpy

@Autowired @Named('alternativeMock')
GreeterService alternativeMock

def "mock service"() {
  def result = serviceMock.greeting

  result == 'mock me'
  1 * serviceMock.getGreeting() >> 'mock me'

def "sub service"() {
  serviceStub.getGreeting() >> 'stub me'

  serviceStub.greeting == 'stub me'

def "spy service"() {
  def result = serviceSpy.greeting

  result == 'Hello World'
  1 * serviceSpy.getGreeting()

def "alternatice mock service"() {
  def result = alternativeMock.greeting

  result == 'mock me'
  1 * alternativeMock.getGreeting() >> 'mock me'

Spring Boot

The recommended way to use Spock mocks in @WebMvcTest tests, is to use an embedded config annotated with @TestConfiguration and to create the mocks using the DetachedMockFactory.

class WebMvcTestIntegrationSpec extends Specification {

  MockMvc mvc

  HelloWorldService helloWorldService

  def "spring context loads for web mvc slice"() {
    helloWorldService.getHelloMessage() >> 'hello world'

    expect: "controller is available"
      .andExpect(content().string("hello world"))

  static class MockConfig {
    def detachedMockFactory = new DetachedMockFactory();

    HelloWorldService helloWorldService() {
      return detachedMockFactory.Stub(HelloWorldService)

For more examples see the specs in the codebase and boot examples.

Tapestry Module

Integration with the Tapestry5 IoC container. For examples see the specs in the codebase.

Unitils Module

Integration with the Unitils library. For examples see the specs in the codebase.

Grails Module

The Grails plugin has moved to its own GitHub project.

Grails 2.3 and higher have built-in Spock support and do not require a plugin.