0

I a trying to mock restetmplate but don't why I am getting the below error. I am using doreturn to verify the invocation of the resttemplate.Is there by other way to mock the rest template and mock the HTTP request

class ToTest{

public ResponseEntity<String>  getValue(){

   if (StringUtils.isNotEmpty(token)) {
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + token);
            headers.setContentType(MediaType.APPLICATION_JSON);

            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(etsServiceResourceDetails.getAccessTokenUri())
                    .queryParam("test","tsetValue");


                builder.queryParam("some","someValue");


            HttpEntity<?> entity = new HttpEntity<>(headers);

            try {
                response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
            }
            catch (RestClientResponseException ex ) {
                log.error(ex);
                if ( ex.getRawStatusCode() == HttpStatus.NOT_FOUND.value()) {

                    return ResponseEntity.notFound().build();
                }
            }
        }
        
        }
        
        }

Unit Test for the rest Temaplate

@Test
    public void testExchange() {

        String token="abbjabxkcbkscksckbckbkcbksckckkcb";
        MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
        queryParams.add("query", "star");
        queryParams.add("test", "star2");

        UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("localhost:8080/some/add").path("/").queryParams(queryParams).buildAndExpand();
        RestTemplate r = new RestTemplate();
        RestTemplate r1 = spy(r);

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<?> entity = new HttpEntity<>(headers);

// ResponseEntity responserm= //r1.exchange(uriComponents.toUriString(),HttpMethod.GET,entity,String.class); //I am getting error org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080 doReturn(new ResponseEntity("", HttpStatus.OK)).when(r1).exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));

        verify(r1, times(1)).exchange(eq(uriComponents.toUriString()), any(), any(), eq(String.class));
        
        assertEquals("check", "http://localhost:8080/some/add/?query=s1&test=s2", uriComponents.toUriString());


    }

Errors

-> at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:581)
Actually, there were zero interactions with this mock.

Wanted but not invoked:
Subham
  • 420
  • 3
  • 11
  • 34
  • The exception tells you what is wrong. There are no interactions. The only thing you do in your code is setup and verification, nothing is being called. – M. Deinum Feb 09 '21 at 07:07
  • I have updated the code. I do try calling the method but an error was thrown – Subham Feb 09 '21 at 07:15
  • @Subham If you want to test your method you should have to rewrite your actual code. It's mean method dependences should pass as method parameters. Later you can write you test case mock and validate the response entity – Rajesh Feb 09 '21 at 07:25
  • @Rajesh Any example would be helpful – Subham Feb 09 '21 at 07:27
  • @Subham https://stackoverflow.com/questions/39486521/how-do-i-mock-a-rest-template-exchange – Rajesh Feb 09 '21 at 07:35
  • There is still no method call you are invoking. Also what is the purpose of this test? Don't you trust the tests from the Spring framework? Why are you testing the `RestTemplate` instead of your own code. – M. Deinum Feb 09 '21 at 07:56

0 Answers0