【发布时间】:2019-03-19 02:14:21
【问题描述】:
我需要模拟一个服务。我在模拟课程时在ResponseEntity<?> resp 中得到空值。
需要模拟的方法:
public List<Expression> getExpression(String expressView, Localdate date) {
List<Expression> =new ArrayList<>();
Map<String, Object> uri = new HashMap<>();
UriComponenetsBuilder build =
UriComponentsBuilder.fromHttpUrl("someUrl" + "/" + expressView);
build.queryParam(someParameter, someParameter);
build.queryParam(someParameter, someParameter);
build.queryParam(someParameter, someParameter);
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
RestClient client = new RestClient(
build.build().encode.toUriString, HttpMethod.GET.Uri, header
);
ResponseEntity<?> resp = restC.SomeMethod(client);
if (resp = !null) {
//it goes to these line
}
}
在我的模拟方法中:
when(restC.SomeMethod(client)).thenReturn(resp);
所以上面的方法调用一个服务获取一些数据,获取expressView的值并保存为列表。当我嘲笑 when(restC.SomeMethod(client)).thenReturn(resp); 方法时,它点击了 URL,但我作为响应得到的值 resp 为 null 。
所以在这里我将resp 的值设为null。我在邮递员中检查了 URL(someUrl) 它返回的值。
如何模拟ResponseEntity<?>?
谢谢。
【问题讨论】:
-
嗨,@J.Macedo 我试过这个例子仍然得到空值。
标签: java web-services spring-boot junit mockito