Resttemplate交换方法的Mockito


@Mock
private RestTemplate restTemplate;
@Test
public void getWebDlpPoliciesTest() throws Exception {
String responseValue = "{n" +
"    "policies": [n" +
"        {n" +
"            "lastUpdatedBy": "15678000",n" +
"            "lastUpdatedTime": "test@gmail.com",n" +
"            "description": "This rule set blocks the transfer of sensitive information outside your organization's network based on DLP Classifications that McAfee maintains and that you can configure.",n" +
"            "publishedStatus": "PUBLISHED",n" +
"            "enabled": "true",n" +
"            "name": "DLP Classification",n" +
"            "id": "DLP_Classification_Rules"n" +
"        }n" +
"tttt]n" +
"}";
ResponseEntity<Object> stringResponse = new ResponseEntity<>(responseValue, HttpStatus.OK);
when(restTemplate.exchange(anyString(), HttpMethod.GET, anyObject(), (Class<Object>) anyObject())).thenReturn(stringResponse);
}

错误:

getting error like org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 3 recorded:

您需要在存根中向HttpMethod.GET添加Mockito.eq(HttpMethod.GET),如下所示

@Mock
private RestTemplate restTemplate;
@Test
public void getWebDlpPoliciesTest() throws Exception {
String responseValue = "{n" +
"    "policies": [n" +
"        {n" +
"            "lastUpdatedBy": "15678000",n" +
"            "lastUpdatedTime": "test@gmail.com",n" +
"            "description": "This rule set blocks the transfer of sensitive information outside your organization's network based on DLP Classifications that McAfee maintains and that you can configure.",n" +
"            "publishedStatus": "PUBLISHED",n" +
"            "enabled": "true",n" +
"            "name": "DLP Classification",n" +
"            "id": "DLP_Classification_Rules"n" +
"        }n" +
"tttt]n" +
"}";
ResponseEntity<Object> stringResponse = new ResponseEntity<>(responseValue, HttpStatus.OK);
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), anyObject(), (Class<Object>) anyObject())).thenReturn(stringResponse);
}

最新更新