gpt4 book ai didi

java - Spring Boot 中 Rest Client Junit 的 InvalidUseOfMatchersException

转载 作者:行者123 更新时间:2023-12-01 16:21:44 26 4
gpt4 key购买 nike

我正在尝试使用mockito为Spring Boot中的Rest Client编写Junit测试用例。在模拟响应时出现错误,如下所示:

使用 Mockito 进行模拟响应:

   UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(Url);

Mockito.when(this.restTlsTemplate
.exchange(Mockito.eq(builder.toString()), HttpMethod.GET, Mockito.any(), RestResponse.class)
.getBody()).thenReturn(response());

获取上述模拟响应时出错:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 2 recorded:
-> at (ApplicationTests.java:54)

This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));

任何人都可以检查一下并帮助我解决这个问题。

最佳答案

如果您对 stub 或验证方法调用中的参数之一使用匹配器,例如 Mockito.eqMockito.any,则必须使用匹配器所有参数。这是因为 Mockito 在内部堆栈中排列其匹配器的方式。所以你应该写

Mockito.when(this.restTlsTemplate
.exchange(Mockito.eq(builder.toString()), Mockito.eq(HttpMethod.GET), Mockito.any(), Mockito.eq(RestResponse.class))
.getBody()).thenReturn(response());

其中 Mockito.eq 已放置在您想要精确匹配的每个参数周围。

关于java - Spring Boot 中 Rest Client Junit 的 InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62255726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com