Mockito PotentialStubbingProblem when stubbing mock for Spring RestTemplate - java

I have been using Mockito in this way for nearly a decade and I am at a loss what is going on here. What am I doing incorrectly here?
The actual call appears correct, but why is my stubbing shown as having empty String and nulls for parameters in the error trace?
Method under test:
private String harvestToken;
private String allUsersUrl;
#Autowired
private RestTemplate restTemplate;
public List<HarvestUser> getActiveHarvestUsers() {
Map<String, String> params = new HashMap<>();
params.put("is_active", "true");
String urlTemplate = UriComponentsBuilder.fromHttpUrl(allUsersUrl).queryParam("is_active", "true")
.toUriString();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.add("Authorization", "Bearer " + harvestToken);
HttpEntity<Void> requestEntity = new HttpEntity<>(httpHeaders);
ResponseEntity<HarvestAllUsers> response = restTemplate.exchange(urlTemplate, HttpMethod.GET, requestEntity,
HarvestAllUsers.class, params);
HarvestAllUsers users = response.getBody();
if (users != null)
return users.getUsers();
else {
log.warn("Harvest users not found!");
return Collections.emptyList();
}
}
Test:
#Mock
RestTemplate mockRestTemplate;
#InjectMocks
HarvestRestClient harvestRestClient;
#Test
void testGetActiveHarvestUsers() {
ReflectionTestUtils.setField(harvestRestClient, "harvestToken", "abc123");
ReflectionTestUtils.setField(harvestRestClient, "allUsersUrl", "http://dummy_url.com");
when(mockRestTemplate.exchange(anyString(), eq(HttpMethod.GET), any(RequestEntity.class),
any(Class.class), any(Map.class))).thenReturn(new ResponseEntity<HarvestAllUsers>(new HarvestAllUsers(), HttpStatus.OK));
List<HarvestUser> result = harvestRestClient.getActiveHarvestUsers();
assertNotNull(result);
}
Note: any(), anyString() and eq() are imported from org.mockito.ArgumentMatchers.*
I also tried using the Mockito.* versions to same effect.
Error trace:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'exchange' method:
mockRestTemplate.exchange(
"http://dummy_url.com?is_active=true",
GET,
<[Content-Type:"application/json", Authorization:"Bearer abc123"]>,
class com.demo.dto.harvest.response.HarvestAllUsers,
{"is_active" = "true"}
);
-> at com.demo.client.HarvestRestClient.getActiveHarvestUsers(HarvestRestClient.java:68)
- has following stubbing(s) with different arguments:
1. mockRestTemplate.exchange(
"",
null,
null,
null,
null
);
-> at com.demo.client.HarvestRestClientTest.testGetActiveHarvestUsers(HarvestRestClientTest.java:52)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
Update:
I was able to get the test to pass by changing the stubbing as follows:
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), ArgumentMatchers.<RequestEntity<Void>>any(), any(Class.class),
any(Map.class))).thenReturn(new ResponseEntity<HarvestAllUsers>(new HarvestAllUsers(), HttpStatus.OK));
But I am keeping this question up because I still have no idea why the original test was failing, and this seems like an unnecessary change.

Related

Mockito unit testing RestTemplate

I am using RestTemplate postForEntity method to post body to an endpoint. I need help with writing test case for my code using Mockito. The return type is void but it can be changed to Types or code if needed to test. I have referred many other documentation but they are very general, I tried using them but most did not work for me as the request and return type are different. . Any suggestions are appreciated. Thank you
Here is my Java class
public void postJson(Set<Type> Types){
try {
String oneString = String.join(",", Types);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("type", oneString);
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(), null);
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<>(request, getHttpHeaders()), String.class);
}
}
}
You are testing the logic inside MyClass class, so you should not mock it. RestTemplate is a dependency inside MyClass, so this is exactly what you need to mock. In general it should look like this inside your test:
This is just a simple example. A good practice would be to check that the arguments passed to your mock equal to the expected ones. One way would be to replace Mockito.eq() with the real expected data. Another is to verify it separately, like this:
public ResponseEntity<String> postJson(Set<Type> Types){
try {
String oneString = String.join(",", Types);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("type", oneString);
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(), null);
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<>(request, getHttpHeaders()), String.class);
}
}
return Types;
You can write test for above method as follows
#Mock
RestTemplate restTemplate;
private Poster poster;
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), getHttpHeaders());
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, request, String.class);
Mockito.verify(restTemplate, Mockito.times(1)).exchange(
Mockito.eq(uri),
Mockito.eq(HttpMethod.POST),
Mockito.eq(request),
Mockito.eq(String.class));
Assert.assertEquals(result, poster.postJson(mockData));
HttpHeaders getHttpHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add(// whatever you need to add);
return headers;
}
Here is my solution to this problem, but it requires some changes.
First of all, you have to externalize the RestTemplate as a bean and add converters in its initialization. This way you will get rid of not covering those converters.
#Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
restTemplate.getMessageConverters().add(jsonConverter);
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
restTemplate.getMessageConverters().add(stringConverter);
return restTemplate;
}
Here's the new class that contains postJson method. As you can see, the url and restTemplate are injected through the constructor. This way we can test different cases.
public class Poster {
private RestTemplate restTemplate;
private String url;
public Poster(RestTemplate restTemplate, String url) {
this.restTemplate = restTemplate;
this.url = url;
}
public void postJson(Set<Type> types) {
try {
String oneString = types.stream().map(Type::toString).collect(Collectors.joining(","));
Map<String, String> requestBody = new HashMap<>();
requestBody.put("type", oneString);
requestBody.put("data", "aws");
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate
.postForEntity(url, new HttpEntity<>(request, getHttpHeaders()), String.class);
int code = result.getStatusCodeValue();
} catch (Exception ignored) {}
}
private HttpHeaders getHttpHeaders() {
return new HttpHeaders();
}
}
And here's the test class for that method.
class PosterTest {
#Mock
private RestTemplate restTemplate;
private String url;
private Poster poster;
#BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
this.url = "http://example.com/posturl";
this.poster = new Poster(restTemplate, url);
}
#Test
void postJson() {
// set input, I used TreeSet just to have a sorted set
// so that I can check the results against
Set<Type> types = new TreeSet<>(Comparator.comparing(Type::toString));
types.add(new Type("a"));
types.add(new Type("b"));
types.add(new Type("c"));
types.add(new Type("d"));
// response entity
ResponseEntity<String> response = ResponseEntity.ok("RESPONSE");
// mockito mock
Mockito.when(restTemplate.postForEntity(
ArgumentMatchers.eq(url),
ArgumentMatchers.any(HttpHeaders.class),
ArgumentMatchers.eq(String.class)
)).thenReturn(response);
// to check the results
Map<String, String> requestBody = new HashMap<>();
requestBody.put("type", "a,b,c,d");
requestBody.put("data", "aws");
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
HttpEntity<HttpEntity<String>> httpEntity = new HttpEntity<>(request, new HttpHeaders());
// actual call
poster.postJson(types);
// verification
Mockito.verify(restTemplate, times(1)).postForEntity(
ArgumentMatchers.eq(url),
ArgumentMatchers.eq(httpEntity),
ArgumentMatchers.eq(String.class));
}
}
It is better to have the dependencies like RestTemplate or other ServiceClasses so that they can be mocked and tested easily.
A while back, I wrote about unit testing and test doubles. You can have a read as a starting point on how to approach unit testing.
Some of it's key take aways are:
Test Behaviour not Implementation. Tests that are independent of implementation details are easier to maintain. In most cases, tests should focus on testing your code’s public API, and your code’s implementation details shouldn’t need to be exposed to tests.
The size of the unit under test is discretionary but the smaller the unit, the better it is.
When talking about unit tests, a more quintessential distinction is whether the unit under test should be sociable or solitary.
A test double is an object that can stand in for a real object in a test, similar to how a stunt double stands in for an actor in a movie. They are test doubles not mocks. A mock is one of the test doubles. Different test doubles have different uses.
It's hard to write a whole test as a lot of information is missing. E.g. what Type is.
As you did not posted the name of your class I'm just name it MyClass for now.
Also I'm assuming that the RestTemplate is injected via the constructor like
MyClass(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
Following is a draft for a unit test using JUnit 5 and Mockito.
I would suggest mocking the RestTemplate. I have to admit that this way we will not cover to test the usage of MappingJackson2HttpMessageConverter and StringHttpMessageConverter in our test.
So a very raw draft could look like this
import java.util.ArrayList;
import java.util.Set;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;
class MyClassTest {
private RestTemplate restTemplate;
private MyClass myClass;
#BeforeEach
void setUp() {
restTemplate = Mockito.mock(RestTemplate.class);
myClass = new MyClass(restTemplate);
}
#Test
void callMethod() {
Set<Type> types = Set.of(/* one of your Types */);
String url = "http://someUrl";
String httpResult = "";
Mockito.when(restTemplate.getMessageConverters()).thenReturn(new ArrayList<>());
ArgumentCaptor<HttpEntity> request = ArgumentCaptor.forClass(HttpEntity.class);
Mockito.when(restTemplate.postForObject(url, request.capture(), String.class)).thenReturn(httpResult);
myClass.callMethod(types, url);
HttpEntity<String> actualHttpEntity = request.getValue();
Assert.assertEquals(actualHttpEntity.getBody(), "");
}
}

How to mock rest template exchange

I have a method in which is it using RestTemplate. I using the following code to make a call:
final ResponseEntity<RESTResponse> responseEntity = restTemplate.exchange(uri,
HttpMethod.POST,
httpEntityWithHeaders,
RESTResponse.class);
httpEntityWithHeads is of type HttpEntity<String>. I am writing a test and trying to mock RestTemplate so that when it calls the exchange method, it will throw an exception.
I am trying to mock it like this:
when(restTemplate.exchange(
ArgumentMatchers.contains(randomHost),
ArgumentMatchers.eq(HttpMethod.POST),
ArgumentMatchers.<HttpEntity<List<String>>>any(),
ArgumentMatchers.<ParameterizedTypeReference<List<RESTResponse>>>any())
).thenThrow(new ResourceAccessException("Random exception message."));
But when running the test, it doesn't throw the exception, it just continues.
Any suggestions?
As you said httpEntityWithHeads is of type HttpEntity<String>, so you have to stub in way that matches to HttpEntity<String>
when(restTemplate.exchange(
ArgumentMatchers.contains(randomHost),
ArgumentMatchers.eq(HttpMethod.POST),
ArgumentMatchers.<HttpEntity<String>>any(),
ArgumentMatchers.<ParameterizedTypeReference<List<RESTResponse>>>any())
).thenThrow(new ResourceAccessException("Random exception message."));
To me seems that your last parameter is not a list it is a class, and that is why the stub is failing, I tried the following and it is working.
#Test(expected = IllegalArgumentException.class)
public void test() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.exchange(anyString(), ArgumentMatchers.eq(HttpMethod.POST),
any(HttpEntity.class),
any(Class.class))).thenThrow(new IllegalArgumentException("a"));
Rest rest = new Rest(restTemplate);
rest.call();
}
public void call(){
HttpEntity<Object> httpEntityWithHeaders= new HttpEntity<>(null);
final ResponseEntity<Object> responseEntity = restTemplate.exchange("a",
HttpMethod.POST,
httpEntityWithHeaders,
Object.class);
}

Mockito mock restTemplate not using returning mock value

Relevant Code Below:
ServiceCode:
#Override
public ResponseEntity<AppointmentResponse> createAppointment(AppointmentRequest partnerFulfillmentRequest) {
RestTemplate rt = null;
ResponseEntity<AppointmentResponse> response = null;
String uri = null;
HttpEntity<AppointmentRequest> httpEntity = null;
HttpHeaders headers = null;
try {
rt = new RestTemplate();
rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
rt.getMessageConverters().add(new StringHttpMessageConverter());
uri = new String(internalServiceUrl+"/"+APP_NAME_INTERNAL+"/appointment");
log.info("Calling internal service URL : "+uri);
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
httpEntity = new HttpEntity<AppointmentRequest>(partnerFulfillmentRequest, headers);
response = rt.exchange(uri, HttpMethod.PUT, httpEntity, AppointmentResponse.class);
if (response != null)
{
log.info("Got response from internal servicec-->statusCode: "+response.getStatusCodeValue());
log.info("Got response from internal service--> Body "+response.getBody());
}
}catch(HttpClientErrorException hceEx) {
//hceEx.printStackTrace();
AppointmentResponse res = new AppointmentResponse();
return new ResponseEntity<AppointmentResponse>(mapResponse(hceEx.getResponseBodyAsString()), hceEx.getResponseHeaders(), hceEx.getStatusCode());
}catch(Exception e) {
e.printStackTrace();
AppointmentResponse res = new AppointmentResponse();
ResponseEntity<AppointmentResponse> wfmErrResponse = new ResponseEntity<AppointmentResponse>(res, HttpStatus.INTERNAL_SERVER_ERROR);
log.error("ERROR WHILE CALLING INTERNAL SERVICE");
log.error(uri);
log.error(e);
return wfmErrResponse;
}
return response;
}
Test Code:
#RunWith(MockitoJUnitRunner.class)
public class PartnerFulfillmentServiceImplTest {
#Mock
RestTemplate restTemplate;
#Mock
HttpHeaders httpHeaders;
#Mock
ResponseEntity responseEntity;
#InjectMocks
PartnerFulfillmentServiceImpl partnerFulfillmentService;
#Test
public void createAppointmentTest() {
Whitebox.setInternalState(partnerFulfillmentService, "internalServiceUrl", "http://localhost:8080");
AppointmentRequest appointmentRequest = new AppointmentRequest();
appointmentRequest.setPartnerName("CENTRICITY");
appointmentRequest.setTicketNumber("123ABC");
httpHeaders = new HttpHeaders();
httpHeaders.set("Content-type", "application/json");
responseEntity = new ResponseEntity<>(
"some response body",
HttpStatus.OK
);
when(restTemplate.exchange(Mockito.anyString(),
Mockito.<HttpMethod> any(),
Mockito.<HttpEntity<?>> any(),
Mockito.<Class<Object>> any()))
.thenReturn(responseEntity);
ResponseEntity<AppointmentResponse> response = partnerFulfillmentService.createAppointment(appointmentRequest);
Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
}
}
I'm getting
java.lang.AssertionError:
Expected :500
Actual :200
and understandably so because it is not actually calling running .thenReturn(responseEntity); logic. My million dollar question is, why? It should be returning the responseEntity value. I have all arguments for the exchange() to any() in hopes to trigger the condition as often as possible as I can always narrow the conditions at a different time. Am I not mocking my restTemplate correctly? That is my current suspicion as to what is going on. Any advice would help!
Thanks!
Like #JB Nizet pointed out, you are creating a new instance of RestTemplate inside your tested method. This means that the exchange method will be called from the new instance, and not a mock. You could implement it the way you did if the class that contains the method createAppointment had a dependency injection of RestTemplate.
What you want there, is to mock the constructor of the new instance of RestTemplate so that, when a new instance would be created, it will be substituted. Unfortunately, Mockito is not capable of mocking a constructor, so you should use PowerMockito for mocking constructors.
whenNew(RestTemplate.class).withNoArguments().thenReturn(restTemplate);
responseEntity = new ResponseEntity<>(
"some response body",
HttpStatus.OK
);
when(restTemplate.exchange(Mockito.anyString(),
Mockito.<HttpMethod> any(),
Mockito.<HttpEntity<?>> any(),
Mockito.<Class<Object>> any()))
.thenReturn(responseEntity);

Mock resttemplate

ResponseEntity<List<AgreementRecord>> myEntity = new
ResponseEntity<List<AgreementRecord>>(HttpStatus.ACCEPTED);
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.<HttpEntity<?>>any(),
ArgumentMatchers.<Class<?>>any())).thenReturn(myEntity);
The rest template returns a list from the application
Eclipse throws a compilation error
The method thenReturn(ResponseEntity) in the type OngoingStubbing>
is not applicable for the arguments
(ResponseEntity>)
Rest template
ResponseEntity<List<AgreementRecord>> responseEntity =
restTemplate.exchange(smoUrl+ GET_AGREEMENT_RECORDS + customerId
,HttpMethod.GET,null,new
ParameterizedTypeReference<List<AgreementRecord>>() {
});
responseEntity.getBody();
One thing you can do is to side-step Mockito's compile-time type checking with doReturn().
doReturn(myEntity)
.when(restTemplate)
.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class)
ArgumentMatchers.<HttpEntity<?>>any(),
ArgumentMatchers.<Class<?>>any()));
This will return myEntity without compile-time type check. if you've got the type wrong, you'll find out as soon as you run your test.
Edit:
#Test()
public void test() {
class AgreementRecord {}
ResponseEntity<List<AgreementRecord>> myEntity = new ResponseEntity<>(
Arrays.asList(new AgreementRecord()), HttpStatus.ACCEPTED);
doReturn(myEntity)
.when(restTemplate)
.exchange(
anyString(),
any(HttpMethod.class),
any(),
any(ParameterizedTypeReference.class));
ResponseEntity<List<AgreementRecord>> responseEntity = restTemplate.exchange(
"url", HttpMethod.GET, null, new ParameterizedTypeReference<List<AgreementRecord>>() {});
List<AgreementRecord> body = responseEntity.getBody();
assertTrue(responseEntity.getStatusCode().is2xxSuccessful());
assertNotNull(body);
}

restTemplate.exchange(url,HttpMethod.POST,entity1,Response.class)) is returned null object instead of responseEntity stubbed data when mockito applied

This is my call to be mocked:
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, entity, Response.class);
mocking like below :
#Test
public void getSpecalityInventoryItemsCallPositiveTest(){
RestTemplateBuilder restTemplateBuilder = Mockito.mock(RestTemplateBuilder.class);
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
GetSpecalityInventoryItemsCaller getSpecalityInventoryItemsCaller = new GetSpecalityInventoryItemsCaller(restTemplateBuilder,id,pw);
Response inventoryItemsRespone = new Response();
inventoryItemsRespone.setStatusCode("0000");
BDDMockito.given(restTemplateBuilder.messageConverters(BDDMockito.any(ArrayList.class))).willReturn(restTemplateBuilder);
BDDMockito.given(restTemplateBuilder.additionalInterceptors(BDDMockito.any(ArrayList.class))).willReturn(restTemplateBuilder);
BDDMockito.given(restTemplateBuilder.build()).willReturn(restTemplate);
ResponseEntity<Response> responseEntity = new ResponseEntity<Response>(inventoryItemsRespone, HttpStatus.OK);
BDDMockito.given(restTemplate.exchange(url,HttpMethod.POST,HttpEntity.EMPTY,Response.class)).willReturn(responseEntity);
Response response = getSpecalityInventoryItemsCaller.getSpecalityInventoryItemsCaller(rxNumber);
Assertions.assertThat(response.getStatusCode()).isEqualTo("0000");
}
but getSpecalityInventoryItemsCaller.getSpecalityInventoryItemsCaller(rxNumber) is executed , inside restTemplate.exchange(url, HttpMethod.POST, entity, Response.class); call is returning null response
i need my own response Response
inventoryItemsRespone = new Response();
inventoryItemsRespone.setStatusCode("0000");
Not getting my own response , please help me out.
My guess is that the stubbing you are doing is matching on exact arguments. The problem is that the actual method may call may include arguments which have the same value as your stubbing but are not the same actual objects.
You should try using matchers for the restTemplate stubbing too:
BDDMockito.given(restTemplate.exchange(any(),any(),any(),any())).willReturn(responseEntity);
Although you might want to make it more precise than the any() matchers I have used here as an example, but it's worth testing to see if this works.

Categories

Resources