想象下面的课
public class ClassToBeTested{
private AnotherClass otherClass;
public void methodToBeTested(){
otherClass = new AnotherClass();
String temp = otherClass.someMethod()
// ...some other code that depends on temp
}
}
现在,如果methodToBeTested被设计为接受 I 的实例,AnotherClass我可以轻松地创建一个模拟AnotherClass并告诉 Mockito 在someMethod()被调用时返回一个我更喜欢的值。但是,由于上述代码是按照 AFAIK 设计的,因此无法模拟AnotherClass和测试此方法将取决于someMethod()返回的内容。
无论如何我可以测试上面的代码而不依赖于someMethod()使用 Mockito 或任何其他框架返回的内容?