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?
Your code works, make sure you have a recent version of
spock-core
the current version is2.0-groovy-3.0
, you also needbyte-buddy
to mock classes instead of just interfaces.