如果匹配器与原始值组合,则可能发生此异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果匹配器与原始值组合,则可能发生此异常相关的知识,希望对你有一定的参考价值。

我的模拟电话如下:

BDDMockito.given(restTemplate.exchange(url, 
    HttpMethod.POST, 
    BDDMockito.any(), 
    Response.class)
).willReturn(responseEntity);

但我得到了以下错误。请帮我解决这个问题???????

4 matchers expected, 1 recorded:
-> at com.esrx.aggregation.service.servicecaller.GetSpecalityInventoryItemsCallerTest.getSpecalityInventoryItemsCallPositiveTest(GetSpecalityInventoryItemsCallerTest.java:67)

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"));

For more info see javadoc for Matchers class.
, mergedContextConfiguration = [WebMergedContextConfiguration@61375dff testClass = GetSpecalityInventoryItemsCallerTest, locations = '{}', classes = '{class com.esrx.aggregation.application.Main, class com.esrx.aggregation.application.Main}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:/application.properties, classpath:/service.properties}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@351d00c0, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@35d019a3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@78691363], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]].
2017-12-19 15:59:53.085  INFO 10140 --- [       Thread-5] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@5a62b2a4: startup date [Tue Dec 19 15:59:35 IST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@3e6f3f28
答案

那是因为你不能将匹配器与原始值混合在一起。如果使用匹配器,则需要对所有参数使用匹配器。如果您仍想匹配精确值,可以使用.eq()匹配器:

BDDMockito.given(restTemplate.exchange(Mockito.eq(url), Mockito.eq(HttpMethod.POST), BDDMockito.any(), Mockito.eq(Response.class))).willReturn(responseEntity)

以上是关于如果匹配器与原始值组合,则可能发生此异常的主要内容,如果未能解决你的问题,请参考以下文章

我想知道像素/片段的原始模板值。零还是一?如果可能的话,得到啥具体的动作来修改模板值?

Laravel查询范围具有动态值

jQuery选择器与JavaScript易出错知识点

Java异常处理机制

我如何在多处理运行时检查值是不是匹配

使用 Java Long 包装器与原始 long 相加数字