我必须嘲笑这项服务
@Service
public class RelationServiceImpl implements RelationService {
@Autowired
private Map<String, Class<? extends Historiable>> modelRelations;
@Override
public Class<? extends Historiable> getModel(String collectionName) {
return modelRelations.get(collectionName);
}
}
但是我不知道如何定义测试的行为。当我尝试这个
@Mock
private RelationService relationService;
@Test
public void getHistoryOk2() throws NotFoundException, ClassNotFoundException {
...
when(relationService.getModel(eq(collectionName))).thenReturn(ShipmentRequest.class);
...
}
我收到这样的编译器错误:
The method thenReturn(Class<capture#1-of ? extends Historiable>) in the type OngoingStubbing<Class<capture#1-of ? extends Historiable>> is not applicable for the arguments (Class<ShipmentRequest>)
但是ShipmentRequest是Historiable的子类
Historiable
|
|-ShipmentRequest
|-...
|-...
我也尝试过这个。而不是在“ thenReturn”部分中使用ShipmentRequest.class,而是调用此方法
when(relationService.getModel(eq(collectionName))).thenReturn(method());
private Class<? extends Historiable> method(){
return ShipmentRequest.class;
}
但是错误变成这样
The method thenReturn(Class<capture#1-of ? extends Historiable>) in the type OngoingStubbing<Class<capture#1-of ? extends Historiable>> is not applicable for the arguments (Class<capture#3-of ? extends Historiable>)
答案 0 :(得分:2)
需要其他强制转换:
when((Class<ShipmentRequest>)relationService.getModel(eq(collectionName))).thenReturn(ShipmentRequest.class);