Skip to main content

Spock VW: writing custom Spock framework extensions

Spock framework has multiple built-in extensions that support many core features like @Ignore and @Timeout annotations. But more importantly developers are encouraged to write their own extensions. For example SpringExtension nicely integrates Spock with Spring framework. Writing custom extensions is not very well documented. In this article we will write very simple extension. It is not a comprehensive guide but just a funny showcase.

Introducing Spock VW extension

In some engineering branches[1] rigorous tests must pass only when external audit is looking. In programming this would be a continuous integration server. Spock VW extension makes sure all tests pass on CI server, even if they fail on developers machine or on production. The idea is heavily inspired by phpunit-vw. Let's take this simple, completely made up test that can't possibly succeed:

class EmissionsSpec extends Specification {

    def 'nitrogen oxide emission (#emission) in #model must not exceed #allowed'() {
            emission <= allowed
            model    | emission || allowed
            'Jetty'  | 1.5      || 0.022
            'Pascal' | 0.67     || 0.016

    def 'carbon dioxide'() {
            105 < 130
First test obviously fails for both samples, but we can transparently add a Spock extension that will make sure no CI server ever catches this issue. The extension simply examines all system properties and environment variables, trying to discover if the host environment is actually a CI server:

package com.nurkiewicz.vw

import org.spockframework.runtime.extension.IGlobalExtension
import org.spockframework.runtime.model.SpecInfo

class VwExtension implements IGlobalExtension {

    private static final CONTROLLED_ENV = [
            'CI', 'CIRCLECI',
            'HUDSON_URL', 'JENKINS_URL',

    private static final boolean EVERYTHING_IS_FINE =
            CONTROLLED_ENV.any {prop ->
                System.getProperty(prop) || System.getenv(prop)}

    void visitSpec(SpecInfo spec) {
        if (EVERYTHING_IS_FINE) {
            spec.features*.skipped = true
VwExtension is like an aspect around every Specification you have in your codebase. It examines a list of known environment variables and if any() of them is present (EVERYTHING_IS_FINE constant), all features (tests) within this Spec are skipped. One more thing. Extensions are not discovered automatically, you must create a special org.spockframework.runtime.extension.IGlobalExtension file under META-INF/services directory on the CLASSPATH (of course it can be in a different JAR). The content of that file is simply a fully qualified name of the extension class, e.g. com.nurkiewicz.vw.VwExtension.

That's about it, happy testing!


  1. This comment has been removed by the author.

  2. Good stuff!

    I created something fairly similar at

  3. This comment has been removed by a blog administrator.


Post a Comment