[英]Testing abstract class with Mockito. How?
I have the following class:我有以下 class:
abstract class Foo {
abstract List<String> getItems();
public void process() {
getItems()
.stream()
.forEach(System.out::println);
}
}
What I'd like to test is the process()
method, but it is dependent on the abstract getItems()
.我想测试的是
process()
方法,但它依赖于抽象的getItems()
。 One solution can be to just create an ad-hoc mocked class that extends Foo
and implements this getItems()
.一种解决方案是创建一个临时模拟的 class 来扩展
Foo
并实现这个getItems()
。
What's the Mockito way to do that? Mockito 的方法是什么?
Why not just:为什么不只是:
List<String> cutsomList = ....
Foo mock = Mockito.mock(Foo.class);
Mockito.when(mock.getItems()).thenReturn(customList);
Mockito.when(mock.process()).thenCallRealMethod();
Or (for void)或(无效)
doCallRealMethod().when(mock.process()).voidFunction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.