Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE

After I upgraded to the newly released 2.2.0.RELEASE version of Spring Boot some of my tests failed. It appears that the MediaType.APPLICATION_JSON_UTF8 has been deprecated and is no longer returned as default content type from controller methods that do not specify the content type explicitly.

Test code like

String content = mockMvc.perform(get("/some-api")             .contentType(MediaType.APPLICATION_JSON))             .andExpect(status().isOk())             .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))             .andReturn()             .getResponse()             .getContentAsString(); 

suddenly did not work anymore as the content type was mismatched as shown below

java.lang.AssertionError: Content type  Expected :application/json;charset=UTF-8 Actual   :application/json 

Changing the code to .andExpect(content().contentType(MediaType.APPLICATION_JSON)) resolved the issue for now.

But now when comparing content to the expected serialized object there is still a mismatch if there are any special characters in the object. It appears that the .getContentAsString() method does not make use of the UTF-8 character encoding by default (any more).

java.lang.AssertionError: Response content expected:<[{"description":"Er hörte leise Schritte hinter sich."}]> but was:<[{"description":"Er hörte leise Schritte hinter sich."}]> Expected :[{"description":"Er hörte leise Schritte hinter sich."}] Actual   :[{"description":"Er hörte leise Schritte hinter sich."}] 

How can I get content in UTF-8 encoding?

like image 310
Times Avatar asked Oct 23 '19 14:10

Times


People also ask

Why is APPLICATION_JSON_UTF8_VALUE deprecated?

APPLICATION_JSON_UTF8_VALUE. Deprecated. as of 5.2 in favor of APPLICATION_JSON_VALUE since major browsers like Chrome now comply with the specification and interpret correctly UTF-8 special characters without requiring a charset=UTF-8 parameter. A String equivalent of APPLICATION_JSON_UTF8 .

What is MockMvc in spring?

What is MockMvc? MockMvc is a Spring Boot test tool class that lets you test controllers without needing to start an HTTP server. In these tests the application context is loaded and you can test the web layer as if i's receiving the requests from the HTTP server without the hustle of actually starting it.

Which class is used to test Spring MVC REST Web application?

We create the test data by using a test data builder class. Configure our mock object to return the created test data when its findAll() method is invoked.


2 Answers

Yes. This is problem from 2.2.0 spring-boot. They set deprecation for default charset encoding.

.getContentAsString(StandardCharsets.UTF_8) - good but in any response would be populated ISO 8859-1 by default.

In my project I updated current created converter:

@Configuration public class SpringConfig implements WebMvcConfigurer {      @Override     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {         converters.stream()             .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)             .findFirst()             .ifPresent(converter -> ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(UTF_8));     } ... 
like image 53
Alexander Someoneperson Avatar answered Sep 23 '22 10:09

Alexander Someoneperson


Using .getContentAsString(StandardCharsets.UTF_8) instead of .getContentAsString() resolves the problem.

like image 32
Times Avatar answered Sep 26 '22 10:09

Times