DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Never Use Credentials in a CI/CD Pipeline Again

Trending

  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Never Use Credentials in a CI/CD Pipeline Again
  1. DZone
  2. Coding
  3. Frameworks
  4. Spock VW: Writing Custom Spock Framework Extensions

Spock VW: Writing Custom Spock Framework Extensions

In some engineering branches, rigorous tests must pass only an external audit; Spock VW extension makes sure all tests pass on a Continuous Integration server

Tomasz Nurkiewicz user avatar by
Tomasz Nurkiewicz
CORE ·
Oct. 11, 15 · Tutorial
Like (4)
Save
Tweet
Share
7.91K Views

Join the DZone community and get the full member experience.

Join For Free

Spock framework has multiple built-in extensions that support many core features like: 

@Ignore , @Timeout and annotations. But more importantly developers are encouraged to write their own extensions. For example, the 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

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:

@Unroll
class EmissionsSpec extends Specification {

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

    def 'carbon dioxide'() {
        expect:
            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 = [
            'bamboo.buildKey',
            'BUILD_ID', 'BUILD_NUMBER', 'BUILDKITE',
            'CI', 'CIRCLECI',
            'CONTINUOUS_INTEGRATION',
            'GOCD_SERVER_HOST',
            'HUDSON_URL', 'JENKINS_URL',
            'TEAMCITY_VERSION',
            'TRAVIS',
    ]

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

    @Override
    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  org.spockframework.runtime.extension.IGlobalExtension in: META-INF; 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!

Spock (testing framework) Framework

Published at DZone with permission of Tomasz Nurkiewicz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Never Use Credentials in a CI/CD Pipeline Again

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: