为简单起见,我们采取一个非常简单的类:
public class TestingClass {
public void method1(){
System.out.println("Running method 1");
method2();
}
public void method2(){
System.out.println("Running method 2");
}
}
现在我正在编写一个简单的测试,该测试检查我们调用method1()
时是否调用了method2()
:
class TestingClassSpec extends Specification {
void "method2() is invoked by method1()"() {
given:
def tesingClass = new TestingClass()
when:
tesingClass.method1()
then:
1 * tesingClass.method2()
}
}
通过执行此测试,我收到以下错误:
运行方法1运行方法2
调用次数太少:
1 * tesingClass.method2()(0调用)
为什么会出现此错误?打印的日志显示已调用[C0]。
测试真实对象上的交互时,您需要使用method2()
,请参见下文:
Spy