【问题标题】:Mockito BDD - mocking RestRemplate exchange() methodMockito BDD - 模拟 RestRemplate exchange() 方法
【发布时间】:2019-03-26 12:25:29
【问题描述】:

我正在尝试使用以下代码模拟 RestTemplate exchange() 调用:

测试方法

given(restTemplate.exchange(any(UriComponents.class), any(HttpMethod.class), any(HttpEntity.class), any(StatusResponse.class)))
            .willReturn(new ResponseEntity<>(HttpStatus.BAD_GATEWAY));

代码无法编译,因为:

  1. 它抱怨cannot resolve method willReturn(new ResponseEntity&lt;&gt;(HttpStatus.BAD_GATEWAY))
  2. 它抱怨cannot resolve method exchange(T, T, T, T)

我应该如何更改签名以使其正常工作?谢谢。

【问题讨论】:

    标签: spring mockito bdd resttemplate


    【解决方案1】:

    看看docs的兑换方式。我没有看到任何在参数中使用UriComponents 的方法。

    交换方法的第一个参数需要使用StringURIRequestEntity

    【讨论】:

    • 是的,我的错。我实际上使用的是builder.toUri(),它返回一个String。我错误地使用了builder 变量的类型。
    【解决方案2】:

    exchange (url) 的第一个参数应该是 eq("url")anyString()(假设“url”是您在测试中使用的值)。

    第四个参数(响应类)应该是eq(StatusResponse.class)any(Class.class)

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2022-01-11
      • 1970-01-01
      • 2020-01-21
      • 2023-03-20
      • 2015-11-04
      相关资源
      最近更新 更多