0
有没有办法让mockito中的模拟或间谍对象在方法竞争前返回其值?例如,如果我有这样的使mockito强制方法在处理完成前返回其值
public class Calculator{
public List calculate(List l){
l.add(1);
l.add(2);
method1(l);
method2(l);
method3(l);
return l;
}
public void method1(List l){
//calculate something here
}
public void method2(List l){
//calculate something here
}
public void method3(List l){
//calculate something here
}
}
级然后我用狙这样
Calculator calculator = new Calculator();
Calculator spy = spy(calculator);
when(spy.calculate(aList)).thenCallRealMethod();
我可以结束其过程中的方法称为方法2之后?我知道我可以使用doNothing().when(spy).method3(anyList())
来避免method3被调用,但有什么方法可以让方法计算在方法2被调用后停止它的进程吗?
_Why_你想在单元测试中做到这一点?你可以添加更多的上下文,因为这看起来像是[X-Y问题](http://mywiki.wooledge.org/XyProblem)? –
,因为我真正的方法是计算数据库中多个实体的方法,一些较早的计算过程可以在不使用数据库的情况下完成,但后面的过程需要更新或从数据库中选择。我希望我的代码覆盖在这个方法中,但作者是其他人,所以如果我可以保持现在的方法,同时仍然保持代码覆盖率。 – Angga
而不是'method1'和'method2'在'List l'上产生_side effects_,你可以重构它们来返回计算结果吗?这样,你可以简单地单独测试这两种方法。 –