我可以模拟部分调用 class 方法吗
Can I mock calling class of method partially
我想模拟调用 class 的方法,在 Mockito 中是否可行
示例代码如下:
private ChecksUtil serivce;
我只想模拟 serivce.method(
) 而不是整个 class.
我该怎么做?
是的,这是可以做到的。你会用间谍。
ChecksUtil service = spy(new ChecksUtil ());
when(service.method()).thenReturn(someObject);
//or if method is a void
doNothing().when(service).method();
间谍对象除了您要模拟的任何方法外,别管对象。
我想模拟调用 class 的方法,在 Mockito 中是否可行
示例代码如下:
private ChecksUtil serivce;
我只想模拟 serivce.method(
) 而不是整个 class.
我该怎么做?
是的,这是可以做到的。你会用间谍。
ChecksUtil service = spy(new ChecksUtil ());
when(service.method()).thenReturn(someObject);
//or if method is a void
doNothing().when(service).method();
间谍对象除了您要模拟的任何方法外,别管对象。