【问题标题】:Why isnt my mocking not working for responseEntity exchange?为什么我的嘲笑不适用于 responseEntity 交换?
【发布时间】: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


    【解决方案1】:

    尝试以下方法:

    when(restTemplate.exchange(anyString(), any(HttpMethod.class), 
       any(HttpEntity.class), eq(AnimalVO.class)).thenReturn(responseEntityMocked)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-16
      • 2011-02-09
      • 2010-09-07
      • 1970-01-01
      • 2019-09-08
      • 2020-12-14
      • 2019-07-31
      • 1970-01-01
      相关资源
      最近更新 更多