【发布时间】:2013-03-14 10:32:32
【问题描述】:
想象下面的类
public class ClassToBeTested{
private AnotherClass otherClass;
public void methodToBeTested(){
otherClass = new AnotherClass();
String temp = otherClass.someMethod()
// ...some other code that depends on temp
}
}
现在,如果 methodToBeTested 被设计为接受 AnotherClass 的实例,我可以轻松地创建 AnotherClass 的模拟,并告诉 Mockito 在调用 someMethod() 时返回我更喜欢的值。然而,由于上述代码是根据 AFAIK 设计的,因此无法模拟 AnotherClass 并且测试此方法将取决于 someMethod() 返回的内容。
有没有我可以测试上面的代码而不依赖于 someMethod() 使用 Mockito 或任何其他框架返回的内容?
【问题讨论】:
标签: java testing mocking mockito