我必须处理没有测试的遗留应用程序。所以在我开始重构之前,我想确保一切都按照原样进行。Mockito说:“通缉,但未被调用”,但调试测试跳转到该方法
现在想象一下以下情况:
public SomeObject doSomething(final OtherObject x, final String something) {
if(x != null) {
final String = someOtherMethod(x, something);
}
}
protected String someOtherMethod(final OtherObject x, final String something) {
....
}
现在我要确保受保护的方法被调用以及
所以,我没有这个
@InjectMocks // Yes there is more going on here
private MyTestObject myTestObject;
private MyTestObject spy;
private static final OtherObject VALID_OTHER_OBJECT = new OtherObject();
@Before
public void setup() {
this.spy = Mockito.spy(myTestObject);
}
@Test
public void ifOtherObjectIsNotNullExpectSubMethodToBeCalled() {
myTestObject.doSomething(VALID_OTHER_OBJECT, null);
verify(spy).someOtherMethod(VALID_OTHER_OBJECT, null);
}
我得到一个失败的测试和“通缉但不被援引...”为someOtherMethod()
。 我跳进调试模式并检查。该方法被称为!
这可能是什么原因造成的?我在这里滥用间谍吗?
编辑:我想拉伸,我知道这不是你通常测试,尤其是因为someOtherMethod(...)
有一个非void返回值在这里。但想象一下返回值是void
...
基本上我只是想了解为什么间谍在这里失败。
它是一个深层复制吗? – Sorona
@Sorona没有在文档中指定,但源代码的快速检查说不。只有浅拷贝。 – Douglas