Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spocklight: Indicate Specification as a Pending Feature

DZone's Guide to

Spocklight: Indicate Specification as a Pending Feature

Working on a specification but don't want to implement it yet? @PendingFeature is a simple Spock annotation that will put your tests on the same page.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Sometimes, we find ourselves working on a new feature in our code and we want to write a specification for it without yet really implementing the feature. To indicate we know the specification will fail while we are implementing the feature, we can add the @PendingFeature annotation to our specification method. With this annotation, Spock will still execute the test, but it will set the status to ignored if the test fails. But if the test passes, the status is set to failed. So when we have finished the feature, we need to remove the annotation — and Spock will kindly remind us to do so this way.

In the following example specification, we use the @PendingFeature annotation:

package sample

import spock.lang.Specification
import spock.lang.PendingFeature
import spock.lang.Subject

class SampleSpec extends Specification {

    @Subject
    private final converter = new Converter()

    @PendingFeature
    void 'new feature to make String upper case'() {
        given:
        def value = 'Spock is awesome'

        expect: // This will fail as expected
        converter.upper(value) == 'SPOCK IS AWESOME'
    }

}

class Converter {
    String upper(String value) {
        value
    }
}


When we run our test in, for example, Gradle we get the following result:

Image title

Now let's implement the upper method:

package sample

import spock.lang.Specification
import spock.lang.PendingFeature
import spock.lang.Subject

class SampleSpec extends Specification {

    @Subject
    private final converter = new Converter()

    @PendingFeature
    void 'new feature to make String upper case'() {
        given:
        def value = 'Spock is awesome'

        expect: // This will fail no more
        converter.upper(value) == 'SPOCK IS AWESOME'
    }

}

class Converter {
    String upper(String value) {
        value.toUpperCase()
    }
}


We run the test again and now we get a failing result although our implementation of the upper method is correct:

Image title

So this tells us the @PendingFeature is no longer needed. We can remove it and the specification will pass correctly.

Written with Spock 1.1.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,spock ,tutorial ,specification ,pendingfeature

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}