0

We're calling an external API that return a simple List, not DTO/Json, like ResponseEntity<List<String>>

In my tests I'm trying to mock the response using mockServer but failed to return the result as List (just String as it not helped me):

mockServer.when(restTemplate.getForObject(any(), any()))
            .respond(HttpResponse.response("{\"40\", \"50\"}")
                    .withStatusCode(200)
                    .withDelay(TimeUnit.SECONDS, 1));

String user = "user";
String password = "password";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));
List<String> retList = restTemplate.getForObject(URI, List.class);

I tried with [, ], {, }, none of them, double parenthesis but failed to return the response as List..

How can I do that correctly?

P.s. in the code all works well with the real API, response came as list as well. The issue is only in the tests, the error is:

"Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [application/octet-stream]"

halfer
  • 19,824
  • 17
  • 99
  • 186
AsfK
  • 3,328
  • 4
  • 36
  • 73
  • 1
    as far as I know, it is only possible if you have a wrapper for the List or use `String[]` instead of the List. – thinkgruen Jun 10 '21 at 10:49
  • Alternatively, maybe this is helpful https://www.baeldung.com/spring-resttemplate-json-list#3-resttemplate-with-user-list-and-parameterizedtypereference – thinkgruen Jun 10 '21 at 11:02
  • The content-type is wrong; probably should be `application/json` instead. – knittl Aug 12 '23 at 20:24

0 Answers0