【发布时间】:2021-11-19 05:16:09
【问题描述】:
在 groovy 中,我尝试使用以下内容来模拟请求的返回,但每当我的代码调用时,我都会收到空指针异常:
ResponseEntity<AnimalVO> result = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headerUtil.headers()), AnimalVO.class);
测试中:
when(restTemplate.exchange(anyString(), any(HttpMethod.class, any(HttpEntity.class, any(ValueObject.class) as Class)).thenReturn(responseEntityMocked)
我正在使用 mockito 3.12 我的测试因空指针异常而失败,而我的 restTemplate 交换(在调试时)返回空值。 我是不是做错了什么?
如果对其他 Temple 交易所有帮助的话有如下定义:
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
对给定的 URI 模板执行 HTTP 方法,将给定的请求实体写入请求,并将响应作为 ResponseEntity 返回。
【问题讨论】:
标签: spring groovy mockito resttemplate