Why does Spock not record calls done by a method of a spied object?

169 Views Asked by At

I am writing an interaction based test with the Help of the Spock Framework, where I would like to check that a method calls another method once.

The class under test looks like this:

class ClassUnderTest {
    def a() {
        b()
    }

    def b() {}
}

And my test case looks like this

class ClassUnderTestTest extends Specification {
    ClassUnderTest cut
    ClassUnderTest spied

    def setup() {
        cut = new ClassUnderTest()
        spied = Spy(cut)
    }

    def "Method a() calls method b()"() {
        when:
        spied.a()

        then:
        1 * spied.a()
        1 * spied.b()
    }
}

Running this test, Spock recognises only the call to a() but not the call to b().

What did I wrong or what did I miss?

1

There are 1 best solutions below

0
On BEST ANSWER

Your code works, make sure you have a recent version of spock-core the current version is 2.0-groovy-3.0, you also need byte-buddy to mock classes instead of just interfaces.