JUnit 4 Module

Integration with JUnit 4 features for Spock 2+ (which internally uses JUnit Platform - part of JUnit 5). Please add dependency org.spockframework:spock-junit4 to your project.

The module is required for:

  • running JUnit 4 rules and class ruless (@Rule/@ClassRule)

  • using JUnit 4’s test fixture annotations (@BeforeClass, @Before, @After, @AfterClass)

This module does its best to support old features from JUnit 4, however, users are encouraged to migrate to the native Spock counterparts.

Guice Module

Integration with the Guice IoC container. Please add dependency org.spockframework:spock-guice to your project. For examples see the specs in the codebase.

With Spock 1.2+ detached mocks are automatically attached to the Specification if they are injected via @Inject.

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. Please add dependency org.spockframework:spock-spring to your project.


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 properly inside the scope of a specification. All interactions with them until they are attached to one, are handled by the default behavior and not recorded.
Furthermore, mocks can only be attached to one Specification instance at a time so keep that in mind when using multi-threaded executions

Java Config

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="https://www.spockframework.org/spring" you get access to the convenience functions for creating mocks.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
           http://www.spockframework.org/spring https://www.spockframework.org/spring/spock.xsd">

  <spock:mock id="serviceMock" class="org.spockframework.spring.docs.GreeterService"/>   (1)
  <spock:stub id="serviceStub" class="org.spockframework.spring.docs.GreeterService"/>   (2)
  <spock:spy id="serviceSpy" class="org.spockframework.spring.docs.GreeterServiceImpl"/> (3)

  <bean id="someExistingBean" class="java.util.ArrayList"/>                              (4)
  <spock:wrapWithSpy ref="someExistingBean"/>                                            (4)

  <bean id="alternativeMock" class="org.spockframework.spring.xml.SpockMockFactoryBean"> (5)
    <constructor-arg value="org.spockframework.spring.docs.GreeterService"/>
    <property name="mockNature" value="MOCK"/>                                           (6)

  1. Creates a Mock

  2. Creates a Stub

  3. Creates a Spy

  4. Wraps an existing bean with a Spy. Fails fast if referenced bean is not found.

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

  6. 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 "alternative mock service"() {
  def result = alternativeMock.greeting

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

Annotation driven

Spock 1.2 adds support for exporting mocks from a Specification into an ApplicationContext. This was inspired by Spring Boot’s @MockBean(realised via Mockito) but adapted to fit into Spock style. It does not require any Spring Boot dependencies, however it requires Spring Framework 4.3.5 or greater to work.

Using @SpringBean

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

To use @SpringBean you have to use a strongly typed field def or Object won’t work. 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 behavior, however they are only picked up once they are attached to the Specification.

@SpringBean definitions can replace existing Beans in your ApplicationContext.

Spock’s @SpringBean actually creates a proxy in the ApplicationContext which forwards everything to the current mock instance. The type of the proxy is determined by the type of the annotated field.
The proxy attaches itself to the current mock in the setup phase, that is why the mock must be created when the field is initialized.
Service1 service1 = Mock()

Service2 service2 = Stub() {
  generateQuickBrownFox() >> "blubb"

def "injection with stubbing works"() {
  service2.generateQuickBrownFox() == "blubb"

def "mocking works was well"() {
  def result = service1.generateString()

  result == "Foo"
  1 * service1.generateString() >> "Foo"
As with Spring’s own @MockBean this will modify your ApplicationContext, and will create an unique context for your Specification preventing it from being reused by Spring’s Context Caching outside of the current Specification.
If you are using a small context this won’t matter much, but if it is a heavy context you might want to use the other approaches, e.g., using the DetachedMockFactory.
Using @SpringSpy

If you want to spy on an existing bean, you can use the @SpringSpy annotation to wrap the bean in a spy. As with @SpringBean the field must be of the type you want to spy on, however you cannot use an initializer.

Service2 service2

Service1 service1

def "default implementation is used"() {
  service1.generateString() == "The quick brown fox jumps over the lazy dog."

def "mocking works was well"() {
  def result = service1.generateString()

  result == "Foo"
  1 * service2.generateQuickBrownFox() >> "Foo"
Using @StubBeans

@StubBeans registers plain Stub instances in an ApplicationContext. Use this if you just need to satisfy some dependencies without actually doing anything with these stubs. If you need to control the stubs, e.g., configure return values then use @SpringBean instead. Like @SpringBean @StubBeans also replaced existing BeanDefinitions,so you can use it to remove real beans from an ApplicationContext. @StubBeans can be replaced by @SpringBean, this can be useful if you need to replace some @StubBeans defined in a parent class.

@ContextConfiguration(classes = DemoMockContext)
class StubBeansExamples extends Specification {

Spring Boot

The recommended way to use Spock mocks in @WebMvcTest or other @SpringBootTest-style tests, is to use the @SpringBean and @SpringSpy annotations as shown above.

Alternatively you can 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.


Spock ignores bean that is not a singleton (in the singleton scope) by default. To enable mocks to work for scoped beans you need to add @ScanScopedBeans to the spec and make sure that the scope allows access to the bean during the setup phase.

The request and session scope will throw exceptions by default, if there is no active request/session.

You can limit the scanning to certain scopes by using the value property of @ScanScopedBeans.

Shared fields injection

Due to certain limitations, injection into shared fields is not enabled by default but can be opted-in to. Refer to javadoc of org.spockframework.spring.EnableSharedInjection for further information.

Tapestry Module

Integration with the Tapestry5 IoC container. Please add dependency org.spockframework:spock-tapestry to your project. For examples see the specs in the codebase.

Unitils Module

Integration with the Unitils library. Please add dependency org.spockframework:spock-unitils to your project. For examples see the specs in the codebase.

Grails Module

The Grails plugin has moved to its own GitHub project. It has legacy status and was last released for Spock 0.7 and Groovy versions 1.8 and 2.0, because it is no longer necessary.

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