【问题标题】:This exception may occur if matchers are combined with raw values如果匹配器与原始值组合,则可能会发生此异常
【发布时间】:2018-06-01 18:31:27
【问题描述】:

我的模拟电话如下:

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

【问题讨论】:

    标签: spring hibernate mockito junit4 spring-mvc-test


    【解决方案1】:

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

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

    【讨论】:

    • BDDMockito.given(restTemplate.exchange(Mockito.anyString(), Mockito.any(), BDDMockito.any(), BDDMockito.>any())).willReturn(响应实体);工作非常感谢!!!!!!
    • 很高兴听到!如果对您有帮助,请接受我的回答/点赞!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多