在与被测类 (CUT) 相同的类中使用 Mockito to Stub 方法
我正在尝试使用Mockito测试一些遗留代码,并且该方法是void类型。
我已经存根了很多对其他类中的方法的调用,这工作正常。但是,我还需要能够在同一类中存根对其他方法的某些调用。
目前这不起作用。
例如,我的类如下所示:
public class Test {
public Test(dummy dummy) {
}
public void checkTask(Task task, List <String> dependencyOnLastSuccessList) throws TaskException {
callToOtherClass.method1 // This works fine, I can stub it using mockito
updateAndReschedule(Long id, String message) // call to method in same class, I cannot stub it
}
public void updateAndReschedule(Long id, String message) {
//method logic.....
}
}
这是我的testClass,展示了我目前拥有的东西:
@Test
public void testMyMethod() {
Test testRef = new Test(taskJob);
Test spy = spy (testRef);
// when a particular method is called, return a specific object
when(callToOtherClass.method1).thenReturn(ObjectABC);
//doNothing when my local method is called
doNothing().when(spy).updateAndReschedule(1, "test");
//make method call
spy.checkTask(ts, taskDependencies);
}