我正在尝试模拟外部调用。
ResponseEntity<?> httpResponse = requestGateway.pushNotification(xtifyRequest);
requestGateway 是一个接口(interface)。
public interface RequestGateway
{
ResponseEntity<?> pushNotification(XtifyRequest xtifyRequest);
}
下面是我正在尝试做的测试方法。
@Test
public void test()
{
ResponseEntity<?> r=new ResponseEntity<>(HttpStatus.ACCEPTED);
when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
}
上面的 when 语句中有一个编译错误,说它是无效的类型。即使 r 是 ResponseEntity 类型。
谁能帮我解决这个问题?
最佳答案
您可以改为使用类型不安全的方法
doReturn(r).when(requestGateway.pushNotification(any(XtifyRequest.class)));
或者您可以在模拟时删除类型信息
ResponseEntity r=new ResponseEntity(HttpStatus.ACCEPTED);
when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
关于Spring 集成框架中 ResponseEntity<?> 的 Junit Mockito 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39015830/