Lets say I have a class Foo
with two void methods bar
and biz(Object object)
public class Foo {
public void bar(){}
public void biz(Object object){}
}
Both methods are complex methods with their own test cases but the problem is bar()
calls biz(Object object)
and I want to verify this in one of bar's test cases. So my test case is set up something like this in Spock.
class FooSpec {
Foo foo = new Foo()
def "test bar"() {
given:
boolean bizCalled = false
foo.metaClass.biz = {Object object -> bizCalled = true}
when:
foo.bar()
then:
0 * _
and:
bizCalled
}
}
I got this solution from this question the problem is that in this example bizCalled
is always false causing the assertion to fail even though I've verified that biz(Object object)
so bizCalled
should be getting set to true.
Is there simply something wrong with the way I'm using metaClass or is there a more correct way in spock to verify that bar()
calls biz(Object object)
in Spock?