简体   繁体   中英

Mockito for Resttemplate exchange method

@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" +
            "\t\t\t\t]\n" +
            "}";
    ResponseEntity<Object> stringResponse = new ResponseEntity<>(responseValue, HttpStatus.OK);
    when(restTemplate.exchange(anyString(), HttpMethod.GET, anyObject(), (Class<Object>) anyObject())).thenReturn(stringResponse);
}

Error:

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

You need to add Mockito.eq(HttpMethod.GET) to the HttpMethod.GET in the stubbing as shown below

@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" +
            "\t\t\t\t]\n" +
            "}";
    ResponseEntity<Object> stringResponse = new ResponseEntity<>(responseValue, HttpStatus.OK);
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), anyObject(), (Class<Object>) anyObject())).thenReturn(stringResponse);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM