I needed to write test case for a method which is having multiple resttemplate.getForObject()
call with different response type. So I wrote like this:
@Test
public void tempMethodTest(){
doReturn(abc).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any());//1st rest call
doReturn(def).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any());//2nd rest call
doReturn(efg).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any());//3rd rest call
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any())).thenReturn(abc);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any())).thenReturn(def);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any())).thenReturn(efg);
assertNotNull(service.tempMethod(obj));
}
But while testing, I am getting below provided exception during first REST call:
com.example.EFG cannot be cast to com.example.ABC
So finally I found solution to this problem.
@Test
public void getBaseStationConfigurationTest(){
when(restTemplate.getForObject(anyString(), any())).thenReturn(abc,def,efg);
assertNotNull(service.tempMethod(obj));
}
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.