例如,有一个带有两个方法methodUnderTest()和display()的类A,其中methodUnderTest调用显示方法。使用Mockito编写junit时,如何模拟display()方法?
class A{
public int method methodUnderTest{
//some code
display();
}
public int display(){
//some code
}
}
最佳答案
您不需要它的模仿。在测试中,当您创建测试对象时,可以通过以下方式创建它:
A underTest = new A() {
@Override
public int display() {
return <expected result>
}
}
这样,您可以控制display方法返回哪种类型的值。