mardi 24 mars 2020

How to deserialize a ClientResponse body in Spring test environment?

I'm trying to create a ClientResponse in test and use it for testing a service, which also does deserialization with standard way response.bodyToMono(..class..). But it appears that there is something wrong in the way I build a fake client response. Because I receive UnsupportedMediaTypeException in tests.

Nevertheless the same code work fine in runtime SpringBoot app, when WebClient returns ClientResponse (which is built internally).

Let's see at the simplest case hich fails with

org.springframework.web.reactive.function.UnsupportedMediaTypeException:
             Content type 'application/json' not supported for bodyType=java.lang.String[]

 void test()
 {
   String body = "[\"a\", \"b\"]";
   ClientResponse response = ClientResponse.create(HttpStatus.OK)
                                           .header(HttpHeaders.CONTENT_TYPE, 
                                                   MediaType.APPLICATION_JSON_VALUE)
                                           .body(body)
                                           .build();

   String[] array = response.bodyToMono(String[].class).block();

   assertEquals(2, array.length);
}

Please, help me to undeerstand, how the client response should be build to allow a standard (json -> object) deserialization in test environment.

Aucun commentaire:

Enregistrer un commentaire