Issue
I have tried many ways to mock the restTemplate exchange, but the mock is not happening, the actual exchange keeps on calling and gives me url not valid exception.
My CallRestService method is below
public class Util{
public static ResponseEntity<String> callRestService(JSONObject reqJsonObj,HttpHeaders headers, String url, HttpMethod method, boolean isAuto){
ResponseEntity<String> re=null;
try{
HttpEntity<String> entity=null;
entity=new HttpEntity<>(String.valueOf(reqJsonObj),headers);
RestTemplate restTemplate= new RestTemplate();
re=restTemplate.exchange(url,method,entity,String.class);
}catch(Exception e){
System.out.println(e);
}
}
}
And My Mock is below:
public class UtilTest{
@InjectMocks
Util util;
@Mock
RestTemplate restTemplate;
ResponseEntity res=mock(ResponseEntity.class);
@Test
public void test(){
//ResponseEntity<String> entity=new ResponseEntity<String>("anySt",HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(HttpEntity.class),
ArgumentMatchers.<Class<String>>any())
).thenReturn(res);
Util.callRestService(json,headers,url,HttpMethod.POST,false);
}
}
I also tried to return the commented response Entity. but always got exception in exchange.
What i understand about mocking is the actual exchange method will not be called, then how am i getting resttemplate exchange exception.
if any inputs required pls comment.
Thanks for the support.
UPDATE: I tried with changing the static method to non static but kept the test case as is. But i get the same error
Solution
The problem is resolved when I update the method to non static and move the Resttemplate declaration outside the method.
Updated code is below, if anyone found it useful.
Method changes :
public class Util{
private RestTemplate restTemplate= new RestTemplate();
public ResponseEntity<String> callRestService(JSONObject reqJsonObj,HttpHeaders headers, String url, HttpMethod method, boolean isAuto){
ResponseEntity<String> re=null;
try{
HttpEntity<String> entity=null;
entity=new HttpEntity<>(String.valueOf(reqJsonObj),headers);
re=restTemplate.exchange(url,method,entity,String.class);
}catch(Exception e){
System.out.println(e);
}
}
}
And the mock with verify :
public class UtilTest{
@InjectMocks
Util util;
@Mock
RestTemplate restTemplate;
@Test
public void test(){
ResponseEntity<String> entity=new ResponseEntity<String>("anySt",HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(HttpEntity.class),
ArgumentMatchers.<Class<String>>any())
).thenReturn(entity);
Util.callRestService(json,headers,url,HttpMethod.POST,false);
Mockito.verify(restTemplate,times(1)).exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(HttpEntity.class),
ArgumentMatchers.<Class<String>>any())
)
}
}
Answered By - nongjai
Answer Checked By - Timothy Miller (JavaFixing Admin)