Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 莫基托打错了_Java_Spring_Mockito_Resttemplate - Fatal编程技术网

Java 莫基托打错了

Java 莫基托打错了,java,spring,mockito,resttemplate,Java,Spring,Mockito,Resttemplate,我在Springrestemplate上为交换调用设置了存根,定义如下: Class dummyResponseType=String.Class; ResponseEntity dummyResponse=mock(ResponseEntity.class); 给定(dummyResponse.getBody()).willReturn(“Hello world”); HttpHeaders responseHeaders=新的HttpHeaders(); 字符串returnedCorrela

我在
Spring
restemplate
上为交换调用设置了存根,定义如下:

Class dummyResponseType=String.Class;
ResponseEntity dummyResponse=mock(ResponseEntity.class);
给定(dummyResponse.getBody()).willReturn(“Hello world”);
HttpHeaders responseHeaders=新的HttpHeaders();
字符串returnedCorrelationId=UUID.randomUUID().toString();
添加(HttpHeaderConstants.CORRELATION\u ID,returnedCorrelationId);
给定(dummyResponse.getHeaders())。将返回(responseHeaders);
给定(restemplate.exchange(anyString()、eq(HttpMethod.GET)、any(RequestEntity.class)、eq(dummyResponseType))。将返回(dummyResponse);
为了获得预定义的响应,我接受任何字符串、get方法、任何请求实体和定义的响应类型

但是我确实遇到了一个潜在的问题:

org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:
 - this invocation of 'exchange' method:
    restTemplate.exchange(
    "http://localhost:8080//api/ping",
    GET,
    <java.lang.Object@3b152928,[X-Correlation-Id:"00e2ab7c-808e-4e1c-b3a9-d65eff7b37ca", Authorization:"Bearer authToken"]>,
    class java.lang.String
);
    -> at 
 - has following stubbing(s) with different arguments:
    1. restTemplate.exchange("", null, null, null);
上的exchange方法已重载,但由于存根中存在类型,这不应造成问题。
我在这里忽略了什么?

对于
restemplate
上的方法,存根的定义是错误的

第三个参数不是
RequestEntity
,而是包含请求的
HttpEntity
。通过此存根更改,可以找到调用

given(restTemplate.exchange(anyString(), any(HttpMethod.class), any(RequestEntity.class), any(Class.class))).willReturn(dummyResponse);