[英]How to mock a non static method of a class whose instance is passed as argument?
如何模拟传递给方法的参数类的非静态方法? 我正在测试以下方法:
public Seed getAppleSeed(AppleTree appleTree){
Seed appleSeed = appleTree.getApple().getSeed();
//Some logic flow
}
其余课程如下:
public class AppleTree{
public Apple getApple(){
return new Apple():
}
}
public class Apple{
public Seed getSeed(){
return new Seed():
}
}
最终目标是测试getAppleSeed()方法的流程,为此我需要模拟对getApple和getSeed的调用。
谢谢
像这样嘲笑:
AppleTree appleTree = Mockito.mock(AppleTree.class);
Apple apple = Mockito.mock(Apple.class);
Seed seed = new Seed();
Mockito.when(appleTree.getApple()).thenReturn(apple);
Mockito.when(apple.getSeed()).thenReturn(seed);
Seed actual = getAppleSeed(appleTree);
assertThat(actual, is(seed));
尽管如果实际代码与您在问题中概述的一样简单,但是我建议您不必模拟Apple
或AppleTree
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.