Skip to content

Improper UTF-8 handling in MockMvc for JSON response #23622

@sgybas

Description

@sgybas

When MediaType.APPLICATION_JSON_UTF8 was deprecated, the content type of JSON that gets sent from a @RestController changed to application/json (without charset).

This breaks MockMvc tests that use .andExpect(content().json()). Here is a sample test:

@Test
public void returnsTheExpectedResponse() throws Exception {
    mockMvc.perform(get("/"))
        .andExpect(status().isOk())
        .andExpect(content().json("{\"name\":\"Jürgen\"}"));
}

The test runs fine with Spring 5.1.9 but fails with Spring 5.2.0.RC2

java.lang.AssertionError: name
Expected: Jürgen
     got: Jürgen

You can find a sample Spring Boot project to reproduce the problem at https://github.com/sgybas/mockmvc-json-utf8

There was a similar issue for jsonPath() (#23219).

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions