mercredi 13 janvier 2021

Spock: How to properly share features between implementations of Specification

I encountered an unfortunate aspect of Spock Framework (1.3-groovy-2.5), which I use for integration testing of a Gradle plugin.

The code sample

Parent class:

class ClassA extends Specification {

    def setupSpec() {
        System.out.println("In ClassA setupSpec()")
    }

    def "base feature"() {
        expect:
        true
    }
}

Child class:

class ClassB extends ClassA {

    @Override
    def setupSpec() {
        System.out.println("In ClassB setupSpec()")
    }

    def "extended feature"() {
        expect:
        true
    }
}

When I run tests in ClassB, both versions of setupSpec() are called:

In ClassA setupSpec()
In ClassB setupSpec()

Of course, if I call method via native Groovy ways:

class Main {

    static void main(String[] args) {
        ClassB classB = new ClassB()
        classB.setupSpec()
    }
}

Then I see only what is expected:

In ClassB setupSpec()

So, evidently, this is a Spock feature of some kind.

Question

In practice, what is the suggested way of inheriting from implementations of Specification while overriding setup logic?

Aucun commentaire:

Enregistrer un commentaire