【发布时间】:2017-12-22 07:23:51
【问题描述】:
我需要模拟一个包含函数 F
要测试的类:
class ClassToBeTested{
@Autowired
DependentClass dependentClass;
public String doSomething(List<Person> list){
...
list.stream().filter(p->p.getAge()>28).
map(dependentClass.transformToEmployee).
collect(Collectors.toList());
...
}
DependentClass sn-p:
public Function<R, C> transformToEmployee = new Function() {
public C apply(R rrd) {
C cc = new C();
if (rrd == null) {
return cc;
} else {
ccInfo.setName(rrd.getFirstName()+ rrd.getLastName());
ccInfo.setAge(rrd.getAge);
return cc;
}
}
};
测试类 sn-p:
@Test
public void testDoSomething(){
...
Function<R, C> info =new TestHelper().transformToEmployee;
info.apply(r);
when(mockedDependentClass.transformToEmployee).thenReturn(info);
...
}
现在我在控制台中看到了这个exception。
when() 需要一个参数,该参数必须是“模拟方法调用”。
【问题讨论】:
-
你考虑过从字段注入改为构造函数注入吗?
-
你为什么要嘲笑这个?只需返回
Function。
标签: unit-testing java-8 mockito junit4