【问题标题】:java.lang.IllegalArgumentException: HttpMethod is required when mocking restTemplate.exhange in JUnit (post call)java.lang.IllegalArgumentException:在 JUnit 中模拟 restTemplate.exchange 时需要 HttpMethod(调用后)
【发布时间】:2021-09-14 18:48:38
【问题描述】:
Mockito.when(restTemplate.exchange(
                anyString(),
                Mockito.eq(HttpMethod.POST),
                Mockito.any(),
                Mockito.eq(String.class)
        )).thenReturn(new ResponseEntity<>(HttpStatus.OK));

像上面一样在 JUnit 中模拟 restTemplate.exchange(POST 方法)。出现以下错误。

我做错了什么? 如何获得这张通行证?

Error.

【问题讨论】:

标签: java spring-boot junit resttemplate


【解决方案1】:

你可以尝试如下。

Mockito.when(restTemplate.exchange(
            ArgumentMatchers.anyString(),
          ArgumentMatchers.any(HttpMethod.class),
          ArgumentMatchers.any(),
          ArgumentMatchers. 
<Class<String>>any())).thenReturn(new ResponseEntity<>
(HttpStatus.OK));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多