0

I have setup a RestTemplate to collect data from an url.

My requirement is to test this code and more importantly the serializer, so given a piece of JSON how do I test that all the values come through to the instances of merchants correctly.

I don't know which serializer is used by RestTemplate to serialize JSON into objects.

RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>("", headers);
ResponseEntity<InboundMerchants> result = template.exchange(
        String.format("%s%s", uri, url),
        HttpMethod.GET,
        request,
        InboundMerchants.class);

InboundMerchants merchants = result.getBody();
return merchants == null
        ? Lists.newArrayList()
        : merchants.getMerchants();
Jim
  • 14,952
  • 15
  • 80
  • 167
  • And what is your exact question now? – GhostCat Jun 09 '19 at 10:00
  • Since you are not using any custom serializer, why do you want to test the serializer which `RestTemplate` is using? It's already a well-tested code. If you have a custom serializer, then I would recommend to write a unit test separately and test it in isolation. – Prateek Jain Jun 09 '19 at 10:18
  • why do you wanna check serializers? better to explain use cases – Ryuzaki L Jun 09 '19 at 14:43
  • the json that was coming back was not getting into the serialized classes correctly. after fiddling with the attributes for a long time, it became apparent that `@JsonProperty` doesn't seem to work for my use case, but `@JsonGetter` and `@JsonSetter` seem to. It would be nice to have a test that takes a JSON string, injects it into the REST pipeline, and see if what comes out the other end is what's expected. It's not about testing the serializer, it's about testing my DTO mappings which were wrong and error prone. – Jim Jun 12 '19 at 06:07

1 Answers1

1

For unit test you can use Mockito if you are using Spring, please check this tutorial: https://www.baeldung.com/spring-mock-rest-template

For integration tests (Your requirement i think) you can use both RestTemplate and MockMvc, please check this thread:

Difference between MockMvc and RestTemplate in integration tests

Mounir Messaoudi
  • 343
  • 1
  • 10
  • this doesn't help me, I need to know which serializer is used, and how to test the serializer. general mocking guidelines is not very helpful. – Jim Jun 09 '19 at 10:31
  • Generaly, in RestTemplate the transformation is done using Jackson. Mybe this links can help you https://stackoverflow.com/questions/9381665/how-can-we-configure-the-internal-jackson-mapper-when-using-resttemplate – Mounir Messaoudi Jun 09 '19 at 11:02
  • https://stackoverflow.com/questions/31883657/customized-objectmapper-not-used-in-test/42854277 – Mounir Messaoudi Jun 09 '19 at 11:06