Mocking RestTemplate#postForObject in a unit test - java

Given a class EncoderService which has the following createNewStream method and a bunch of constants used in the method, how can I use mockito to write a unit-test for the createNewStream method:
public ResponseEntity<Object> createNewStream(Long channelId) {
String url = IP + VERSION + serverName + VHOSTS + vhostName + APP_NAME + appName + STREAM_FILES;
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.setAcceptCharset(Arrays.asList(Charset.forName(UTF_8)));
RestTemplate restTemplate = new RestTemplate();
String udp = "udp://" + "localhost" + ":" + "1935";
Map<String, String> map = new HashMap<>();
map.put("name", STREAMS + appName + channelId);
map.put("serverName", serverName);
map.put("uri", udp);
HttpEntity<Map<String, String>> request = new HttpEntity<>(map, headers);
HttpStatus statusCode = null;
try {
ResponseEntity<Object> response = restTemplate.postForEntity(url, request, Object.class);
statusCode = response.getStatusCode();
map.put(MESSAGE, "successful");
return new ResponseEntity<>(map, statusCode);
} catch (HttpStatusCodeException e) {
map.put(MESSAGE, e.getMessage());
return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);
}
}

RestTemplate is a class, not an interface, and it implements the actual HTTP transport. Both are standing in the way of writing a testable method. On top of that the fact that you are constructing an instance of a class that has side effects on the OS level rather than getting it injected does not help the case. So the way to solve it is:
write your method based around an interface rather than an implementation, RestOperations in this case
inject an instance implementing RestOperations, e.g. an instance of RestTemplate for production, via a constructor argument (preferred), method argument or via a Supplier<RestOperations> defined as a field on the class
substitute an actual instance with a test implementation or a mock in test. I guess it is easier to go for a Mockito.mock(RestOperations.class) because RestOperations just like all other Spring interfaces defines way too many method for writing a test implementation manually
So in EncoderService you can have:
private final RestOperations restClient;
public EncoderService(RestOperations restClient) {
this.restClient = restClient;
}
public ResponseEntity<Object> createNewStream(Long channelId) {
...
ResponseEntity<Object> response = restClient.postForEntity(...
...
}
And then in EncoderServiceTest:
ResponseEntity<Object> expectedReturnValue = ...
RestOperations testClient = mock(RestOperations.class);
doReturn(expectedReturnValue).when(testClient).postForEntity(any(), any(), anyClass());
EncoderService service = new EncoderService(testClient);
// use the service
For the other two cases the test setup is exactly the same, just you would pass the instance into the method call instead of constructor or overwrite the supplier on the EncoderService instance to return the testClient.
I have answered a very similar question about ProcessBuilder which also has side effects on the OS level and was constructed directly in the method under test here Error trying to mock constructor for ProcessBuilder using PowerMockito You can apply exactly the same tactics.

Related

Spring Boot integration test. How to test the 'DELETE' method?

I'm using Spring Boot 2.5.6 and JUnit 4.13.2. My task is to test the DELETE method
My REST controller:
#RestController
public class DomainEndpoint {
private final SomeService service;
#DeleteMapping("/domain/{id}")
public void delete(#PathVariable long id) {
service.delete(id);
}
}
My test:
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#RunWith(SpringRunner.class)
public class DomainEndpointTest {
#Autowired
TestRestTemplate template;
#MockBean
SomeService service;
#Test
public void delete() {
String url = "/domain/123";
ResponseEntity<?> resp = template.exchange(url, HttpMethod.DELETE, new HttpEntity<>(""), String.class);
assertEquals(HttpStatus.NO_CONTENT, resp.getStatusCode());
}
}
As you can see, the only solution for testing the 'DELETE' method, which I found, is:
ResponseEntity<?> resp = template.exchange(url, HttpMethod.DELETE, new HttpEntity<>(""), String.class);
But params for body new HttpEntity<>("") and for return type String.class seem strange to me. Why should I use them? Can I do the same more straightway without passing unnecessary parameters?
On the other hand, TestRestTemplate template has a set of short and readable methods delete(). The problem with them - they return void and I can't check the response status code in this case.
The main question is how to test DELETE methods correctly?
Instead of passing in new HttpEntity<>("") HttpEntity has a special class variable you can use called HttpEntity.EMPTY for these situations. You can also use a return type of void.
ResponseEntity<Void> resp = template.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);
Two things you could improve:
Don't provide a request entity – it is marked as #Nullable
Use Void.class as return type to express that you don't expect any response body
ResponseEntity<Void> resp = restTemplate.exchange(url, HttpMethod.DELETE, null, Void.class);
Others have given different options but if you want to use the the delete method - it internally handles the http error by throwing runtime error as you can see here. You can check for HttpClientErrorException or HttpServerErrorException by using assertDoesNotThrow from JUnit5
In my opinion RestTemplate.delete method is the one to use, return type is void but status other than 2XX will throw an exception.

Deserializing interfaces with Rest controller in Java Spring framework

I have inherited a simple enough controller. The problem is that it returns interface and Spring AFAIK cannot deserialize if it can't instantiate. I can fix it with one annotation on (each) transport interface, but it removes any benefit of returning an interface as opposed to a concrete class and steps out of bounds of DTO responsibilities.
Therefore my question is can something be done on the client side to consume controller as it is? If not, can something be done on controller code without altering UserDto interface?
Here's the Rest controller:
#RestController
#RequestMapping(value = "/user")
public class UserController {
// ...
#RequestMapping(value = "/create", method = RequestMethod.POST)
public UserDto create(#RequestBody UserDto user) throws ServiceException {
return service.save(user);
}
// ...
}
Here's the test that acts as a client and consumes the controller:
#Test
public void UserControllerRealTest() {
ClientUser user1 = new ClientUserImpl("ADSMarko", "Marko Savić", "Slayer!!!");
RestTemplate rt = new RestTemplate();
HttpEntity<ClientUser> request = new HttpEntity<>(user1);
UserDto result1 = rt.postForObject(createUrl, request, UserDtoImpl.class);
System.out.println("User " + result1.getUsername() + " saved with id " + result1.getId());
ClientUser user2 = new ClientUserImpl("Marko", "Marko Savić", "Slayer!!!");
request = new HttpEntity<>(user2);
ParameterizedTypeReference<UserDto> ptr = new ParameterizedTypeReference<UserDto>() {};
ResponseEntity<UserDto> re = rt.exchange(createUrl, HttpMethod.POST, request, ptr);
UserDto result2 = re.getBody();
System.out.println("User " + result2.getUsername() + " saved with id " + result2.getId());
}
Bellow is the interface in question. It works with the leading annotation, but without it throws com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
#JsonDeserialize(as = UserDtoImpl.class) // Or the longer annotation
public interface UserDto {
// ... just getters and setters
}

How to mock RequestEntity.put() and restTemplate.exchange() in Spock groovy

Java code for api call
I want to know how to test the below two lines of code.
private void api(){
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
RestTemplate restTemplate = new RestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
//And this line
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity,String.class);
HttpStatus StatusCode = responseEntity.getStatusCode();
}
This is not a Spock question as such (you didn't present a single line of Spock specification, so nobody knows what you have tried so far), rather a software engineering or general testing question. The problem with testing spaghetti code - and here I mean code which both does something and creates lots of objects at the same time - is that from the outside there is no access to objects created inside a method and stored in a local variable. There are two ways to refactor your code for better testability:
If it makes sense, change the code so as to enable the user to inject the dependencies from the outside instead of the code creating them internally. Please google "dependency injection" and find variants like
constructor injection,
setter injection,
method parameter injection,
field injection when using tools/paradigms like CDI.
Another way is to factor out the object-creating parts of the method into smaller producer (or creator or factory) methods which then you can overwrite (stub) according to your choice in your test when using a partial mock (spy) object. Spock provides such spies, so you can easily use them.
I am going to show the latter approach for your information:
package de.scrum_master.stackoverflow.q58101434;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class MyRestApi {
public HttpStatus api() throws URISyntaxException {
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = createHttpHeaders();
RestTemplate restTemplate = createRestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = createRequestEntity(url, body);
//And this line
ResponseEntity<String> responseEntity = executeRequest(restTemplate, requestEntity);
return responseEntity.getStatusCode();
}
HttpHeaders createHttpHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return httpHeaders;
}
RestTemplate createRestTemplate() {
return new RestTemplate();
}
RequestEntity<String> createRequestEntity(String url, String body) throws URISyntaxException {
return RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
}
ResponseEntity<String> executeRequest(RestTemplate restTemplate, RequestEntity<String> requestEntity) {
return restTemplate.exchange(requestEntity,String.class);
}
}
See how the method is more structured and more readable now (could still be improved, I just did it in a quick & dirty way)? Please especially notice the helper methods createRequestEntity and executeRequest.
Now here is how you can write your Spock test:
package de.scrum_master.stackoverflow.q58101434
import org.springframework.http.HttpStatus
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import spock.lang.Specification
import spock.lang.Unroll
class MyRestApiTest extends Specification {
#Unroll
def "API returns status code #statusCode"() {
given: "prepare mocks + spy"
RequestEntity<String> requestEntity = Mock()
ResponseEntity<String> responseEntity = Mock() {
getStatusCode() >> httpStatus
}
MyRestApi myRestApi = Spy() {
createRequestEntity(_, _) >> requestEntity
executeRequest(_, _) >> responseEntity
}
when: "execute API method"
def result = myRestApi.api()
then: "check expected results"
// This actually only tests mockfunctionality, your real test would look differently
statusCode == result.value()
reasonPhrase == result.reasonPhrase
where:
httpStatus | statusCode | reasonPhrase
HttpStatus.OK | 200 | "OK"
HttpStatus.MOVED_PERMANENTLY | 301 | "Moved Permanently"
HttpStatus.UNAUTHORIZED | 401 | "Unauthorized"
}
}
Feel free to ask (related!) follow-up questions if you do not understand this code. I advise you to learn more about clean code, testability, mock testing in general and also about Spock in particular.

Unit test for a header created inside controller

In my Spring Boot application, I'm adding a header inside my controller:
#RequestMapping(value = "/people", method = RequestMethod.GET)
public ResponseEntity<List<Person>> systems(Pageable pageable) {
Page people = peopleService.getPeopleSystems(pageable);
HttpHeaders headers = new HttpHeaders();
headers.add("CustomHeader1", String.valueOf(people.getTotalElements()));
headers.add("CustomHeader2", String.valueOf(people.getSize()));
return new ResponseEntity<List<People>>(people.getContent(), headers, HttpStatus.OK);
}
The header contains the quantity of people which is obtained inside controller.
The code works as expected, but isn't testable.
The test class roughly could be like this:
#Test
public void test_header() throws Exception {
PeopleService pepleService = mock(PeopleService.class);
Pageable pageable = mock(Pageable.class);
Page<Person> page = mock(Page.class);
when(pepleService.getPeopleSystems(pageable)).thenReturn(page);
when(page.getTotalElements()).thenReturn((long) 2000);
when(page.getSize()).thenReturn(10);
mockMvc.perform(get("/people")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(header().stringValues("CustomHeader1"));
}
The test fails at line
headers.add("CustomHeader1", String.valueOf(people.getTotalElements()));
with NPE exception.
So my question is how to make my controller testable, or how to refactor the code to get rid of creating the new HttpHeaders() inside my method.
Any help would be apreciated.
Turned out that all what I needed was to indicate that the class was instance of Pagable.class. Here is the working test:
Page<Person> page = new PageImpl<>(new ArrayList<>());
when(pepleService.getPeopleSystems(any(Pageable.class))).thenReturn(page);
mockMvc.perform(get("/people")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(header().string("CustomHeader1", "2000"));
You need to mock peopleService and then mock method peopleService.getPeopleSystems(pageable) to return mock of Page.
Then you need to mock methods people.getTotalElements() and people.getSize() to return proper values.
HttpHeaders headers = mock(HttpHeaders.class) this should not work because new object is created in method.

How do I mock a REST template exchange?

I have a service in which I need to ask an outside server via rest for some information:
public class SomeService {
public List<ObjectA> getListofObjectsA() {
List<ObjectA> objectAList = new ArrayList<ObjectA>();
ParameterizedTypeReference<List<ObjectA>> typeRef = new ParameterizedTypeReference<List<ObjectA>>() {};
ResponseEntity<List<ObjectA>> responseEntity = restTemplate.exchange("/objects/get-objectA", HttpMethod.POST, new HttpEntity<>(ObjectAList), typeRef);
return responseEntity.getBody();
}
}
How can I write a JUnit test for getListofObjectsA()?
I have tried with the below:
#RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
private MockRestServiceServer mockServer;
#Mock
private RestTemplate restTemplate;
#Inject
private SomeService underTest;
#Before
public void setup() {
mockServer = MockRestServiceServer.createServer(restTemplate);
underTest = new SomeService(restTemplate);
mockServer.expect(requestTo("/objects/get-objectA")).andExpect(method(HttpMethod.POST))
.andRespond(withSuccess("{json list response}", MediaType.APPLICATION_JSON));
}
#Test
public void testGetObjectAList() {
List<ObjectA> res = underTest.getListofObjectsA();
Assert.assertEquals(myobjectA, res.get(0));
}
However the above code does not work, it shows that responseEntitty is null. How can I correct my test to properly mock restTemplate.exchange?
This is an example with the non deprecated ArgumentMatchers class
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(),
ArgumentMatchers.<Class<String>>any()))
.thenReturn(responseEntity);
You don't need MockRestServiceServer object. The annotation is #InjectMocks not #Inject. Below is an example code that should work
#RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
#Mock
private RestTemplate restTemplate;
#InjectMocks
private SomeService underTest;
#Test
public void testGetObjectAList() {
ObjectA myobjectA = new ObjectA();
//define the entity you want the exchange to return
ResponseEntity<List<ObjectA>> myEntity = new ResponseEntity<List<ObjectA>>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/objects/get-objectA"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<ObjectA>>>any(),
Matchers.<ParameterizedTypeReference<List<ObjectA>>>any())
).thenReturn(myEntity);
List<ObjectA> res = underTest.getListofObjectsA();
Assert.assertEquals(myobjectA, res.get(0));
}
ResponseEntity<String> responseEntity = new ResponseEntity<String>("sampleBodyString", HttpStatus.ACCEPTED);
when(restTemplate.exchange(
Matchers.anyString(),
Matchers.any(HttpMethod.class),
Matchers.<HttpEntity<?>> any(),
Matchers.<Class<String>> any()
)
).thenReturn(responseEntity);
For me, I had to use Matchers.any(URI.class)
Mockito.when(restTemplate.exchange(Matchers.any(URI.class), Matchers.any(HttpMethod.class), Matchers.<HttpEntity<?>> any(), Matchers.<Class<Object>> any())).thenReturn(myEntity);
This work on my side.
ResourceBean resourceBean = initResourceBean();
ResponseEntity<ResourceBean> responseEntity
= new ResponseEntity<ResourceBean>(resourceBean, HttpStatus.ACCEPTED);
when(restTemplate.exchange(
Matchers.anyObject(),
Matchers.any(HttpMethod.class),
Matchers.<HttpEntity> any(),
Matchers.<Class<ResourceBean>> any())
).thenReturn(responseEntity);
The RestTemplate instance has to be a real object. It should work if you create a real instance of RestTemplate and make it #Spy.
#Spy
private RestTemplate restTemplate = new RestTemplate();
I used to get such an error. I found a more reliable solution. I have mentioned the import statements too which have worked for me. The below piece of code perfectly mocks restemplate.
import org.mockito.Matchers;
import static org.mockito.Matchers.any;
HttpHeaders headers = new Headers();
headers.setExpires(10000L);
ResponseEntity<String> responseEntity = new ResponseEntity<>("dummyString", headers, HttpStatus.OK);
when(restTemplate.exchange( Matchers.anyString(),
Matchers.any(HttpMethod.class),
Matchers.<HttpEntity<?>> any(),
Matchers.<Class<String>> any())).thenReturn(responseEntity);
If anyone has this problem while trying to mock restTemplate.exchange(...), the problem seems to be with matchers. As an example: the following won't work,
when(ecocashRestTemplate.exchange(Mockito.any()
, Mockito.eq(HttpMethod.GET)
, Mockito.any(HttpEntity.class)
, Mockito.<Class<UserTransaction>>any())
).thenReturn(new ResponseEntity<>(transaction, HttpStatus.OK));
but this one will actually work:
ResponseEntity<UserTransaction> variable = new ResponseEntity<>(transaction, HttpStatus.OK);
when(ecocashRestTemplate.exchange(Mockito.anyString()
, Mockito.eq(HttpMethod.GET)
, Mockito.any(HttpEntity.class)
, Mockito.<Class<UserTransaction>>any())
).thenReturn(new ResponseEntity<>(transaction, HttpStatus.OK));
NOTICE the Mockito.anyString() on the second block vs theMockito.any().
Let say you have an exchange call like below:
String url = "/zzz/{accountNumber}";
Optional<AccountResponse> accResponse = Optional.ofNullable(accountNumber)
.map(account -> {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "bearer 121212");
HttpEntity<Object> entity = new HttpEntity<>(headers);
ResponseEntity<AccountResponse> response = template.exchange(
url,
GET,
entity,
AccountResponse.class,
accountNumber
);
return response.getBody();
});
To mock this in your test case you can use mocitko as below:
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(),
ArgumentMatchers.<Class<AccountResponse>>any(),
ArgumentMatchers.<ParameterizedTypeReference<List<Object>>>any())
)
When we are testing a Client which is communicating to some external system using restTemplate, as part of unit tests we need to verify the httpEntity, headers and parameters which we are sending.
ArgumentCaptor comes handy in these situation. So here is my example (working code)
#Mock
private RestTemplate restTemplate;
#InjectMocks
private MyClient client;
#Captor
ArgumentCaptor<HttpEntity<?>> httpEntityCaptor;
when(restTemplate.exchange(eq(expectedUrl), eq(HttpMethod.POST), Matchers.any(HttpEntity.class), eq(MyTargetResponse.class)).thenReturn(expectedResponse);
verify(restTemplate).exchange(eq(expectedUrl),eq(HttpMethod.POST), httpEntityCaptor.captor(),eq(MyTargetResponse.class));
HttpEntity<?> actualResponse = httpEntityCaptor.getValue();
HttpHeaders actualResponse.getHeaders();
assertEquals(headers.getFirst("Content-Type", "application/json")
Now assertions can be made based on your use case, as you have got the captured object which was sent.
You can use below non deprecated ArgumentMatchers
lenient().when(restTemplate.exchange(ArgumentMatchers.any(String.class),
ArgumentMatchers.eq(HttpMethod.GET),
ArgumentMatchers.any(),
ArgumentMatchers.eq(new ParameterizedTypeReference<List<ObjectA>>() {
})))
.thenReturn(responseEntity);
I implemented a small library that is quite useful. It provides a ClientHttpRequestFactory that can receive some context. By doing so, it allows to go through all client layers such as checking that query parameters are valued, headers set, and check that deserialization works well.
If you are using RestTemplateBuilder may be the usual thing wouldn't work. You need to add this in your test class along with when(condition).
#Before
public void setup() {
ReflectionTestUtils.setField(service, "restTemplate", restTemplate);
}
If anyone is still facing this issue, Captor annotation worked for me
#Captor
private ArgumentCaptor<Object> argumentCaptor;
Then I was able to mock the request by:
ResponseEntity<YourTestResponse> testEntity = new ResponseEntity<>(
getTestFactoryResponse(),
HttpStatus.OK);
when(mockRestTemplate.exchange((String) argumentCaptor.capture(),
(HttpMethod) argumentCaptor.capture(),
(HttpEntity<?>) argumentCaptor.capture(),
(Class<YourTestResponse.class>) any())
).thenReturn(testEntity);
For this specific exchange() case,
I found that easier just stub it instead, good old override:
var restTemplate = new RestTemplate() {
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, #Nullable HttpEntity<?> requestEntity,
Class<T> responseType) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
}
};
less mock stuff.. especially that api is always changing. eq(..) any().. etc.
you can check on arg inside of your stubbed exchange() before returning something or throwing exception.
--
I know that it is not the answer to that strict question. But same result. less code & easier to support.
With mockito-core-2.23.4
ResponseEntity<YOUR_CLASS> responseEntity = new ResponseEntity(YOUR_CLASS_OBJECT, HttpStatus.OK);
when(restTemplate.exchange(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.<ParameterizedTypeReference<YOUR_CLASS>> any()))
.thenReturn(responseEntity);
Was get it working this way - Mockito.when(restTemplate.exchange((URI) any(), (HttpMethod) any(), (HttpEntity<?>) any(), (Class) any()))
.thenReturn(responseEntity);
However the above code does not work, it shows that responseEntitty is
null. How can I correct my test to properly mock
restTemplate.exchange?
It returns null because using:
#Mock
private RestTemplate restTemplate;
If the goal is to mock with MockRestServiceServer instead of Mockito, it should be:
#Autowired
private RestTemplate restTemplate;
or RestTemplate restTemplate = new RestTemplate()
when the same instance is provided to MockRestServiceServer and SomeService.
For example:
#Test
public void methodWithPostCallTest() throws URISyntaxException {
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(ExpectedCount.once(),
requestTo(new URI("post-method-url")))
.andExpect(method(HttpMethod.POST))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body("response-body")
);
YourService yourService = new YourService(restTemplate);
String response = yourService.methodWhichExecutesPostCall();
mockServer.verify();
assertEquals("response-body", response);
}
Using MockRestServiceServer, it would return mock response for any RestTemplate method for a POST call - postForEntity, postForObject or exchange.
More details: Testing Client Applications
If your intention is test the service without care about the rest call, I will suggest to not use any annotation in your unit test to simplify the test.
So, my suggestion is refactor your service to receive the resttemplate using injection constructor. This will facilitate the test. Example:
#Service
class SomeService {
#AutoWired
SomeService(TestTemplateObjects restTemplateObjects) {
this.restTemplateObjects = restTemplateObjects;
}
}
The RestTemplate as component, to be injected and mocked after:
#Component
public class RestTemplateObjects {
private final RestTemplate restTemplate;
public RestTemplateObjects () {
this.restTemplate = new RestTemplate();
// you can add extra setup the restTemplate here, like errorHandler or converters
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
}
And the test:
public void test() {
when(mockedRestTemplateObject.get).thenReturn(mockRestTemplate);
//mock restTemplate.exchange
when(mockRestTemplate.exchange(...)).thenReturn(mockedResponseEntity);
SomeService someService = new SomeService(mockedRestTemplateObject);
someService.getListofObjectsA();
}
In this way, you have direct access to mock the rest template by the SomeService constructor.

Categories