AK_is_curious
AK_is_curious

Reputation: 239

Run specific Spock Tests without setup?

In the documentation Spock mentions these function in the section 'Fixture Methods':

def setup() {}          // run before every feature method
def cleanup() {}        // run after every feature method
def setupSpec() {}     // run before the first feature method
def cleanupSpec() {}   // run after the last feature method

I am using the setup function to initialize new objects for all tests. However some tests are supposed to test what happens when there are no other objects yet.

Because of the setup these tests fail now.

Is there any way to suspend the setup for certain functions? An annotation perhaps?

Or do I have to create a separate "setup" function and call them in each test I do. The majority of tests uses it!

Upvotes: 2

Views: 1812

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42234

You can always override value inside your test method only for that specific method. Take a look at following example:

import spock.lang.Shared
import spock.lang.Specification

class SpockDemoSpec extends Specification {

    String a
    String b

    @Shared
    String c

    def setup() {
        println "This method is executed before each specification"
        a = "A value"
        b = "B value"
    }

    def setupSpec() {
        println "This method is executed only one time before all other specifications"
        c = "C value"
    }

    def "A empty"() {
        setup:
        a = ""

        when:
        def result = doSomething(a)

        then:
        result == ""
    }

    def "A"() {
        when:
        def result = doSomething(a)

        then:
        result == "A VALUE"

    }

    def "A NullPointerException"() {
        setup:
        a = null

        when:
        def result = doSomething(a)

        then:
        thrown NullPointerException
    }

    def "C"() {
        when:
        def result = doSomething(c)

        then:
        result == 'C VALUE'
    }

    private String doSomething(String str) {
        return str.toUpperCase()
    }
}

In this example a and b are set up before each test and c is set up only once before any specification is executed (that's why it requires @Shared annotation to keep it's value between executions).

When we run this test we will see following output in the console:

This method is executed only one time before all other specifications
This method is executed before each specification
This method is executed before each specification
This method is executed before each specification
This method is executed before each specification

The general rule of thumb is that you shouldn't change c value after it is set up in setupSpec method - mostly because you don't know if which order tests are going to be executed (in case you need to guarantee the order you can annotate your class with @Stepwise annotation - Spock will run all cases in order they are defined in this case). In case of a and b you can override then at the single method level - they will get reinitialized before another test case is executed. Hope it helps.

Upvotes: 2

Related Questions