我想测试一些在同一类中调用其他方法的方法。它们基本上是相同的方法,但是具有不同数量的参数,因为数据库中有一些默认值。我在这个上展示
public class A{
Integer quantity;
Integer price;
A(Integer q, Integer v){
this quantity = q;
this.price = p;
}
public Float getPriceForOne(){
return price/quantity;
}
public Float getPrice(int quantity){
return getPriceForOne()*quantity;
}
}
因此,我想测试在调用方法getPrice(int)时是否调用了方法getPriceForOne()。基本上像平常一样调用getPrice(int)并模拟getPriceForOne。
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
....
public class MyTests {
A mockedA = createMockA();
@Test
public void getPriceTest(){
A a = new A(3,15);
... test logic of method without mock ...
mockedA.getPrice(2);
verify(mockedA, times(1)).getPriceForOne();
}
}
请记住,我有一个更复杂的文件,对其他人来说是一个实用程序,它们必须全部放在一个文件中。
您将需要一个间谍,而不是模拟A:
A a = Mockito.spy(new A(1,1));
a.getPrice(2);
verify(a, times(1)).getPriceForOne();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句