MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding MockMvc and UTF-8 Handling in Spring Boot 2.2.0.RELEASE
Spring Boot and Spring Framework have long been trusted tools for Java developers, offering a comprehensive suite of utilities designed to simplify the development process. However, with the release of Spring Boot 2.2.0.RELEASE, developers began to see unexpected behavior concerning the handling of UTF-8 characters when using MockMvc for testing. In this article, we will explore the technical underpinnings of this issue, offer examples, and review some possible workarounds.
What is MockMvc?
`MockMvc` is a part of the Spring Test framework that allows you to test your Spring MVC application through a fluent API. It provides a way to execute HTTP requests in a test environment and simulate interactions with the server-side components, without the browser. This testing utility is invaluable during development for ensuring that your web application behaves as expected under various conditions.
UTF-8 Encoding in Web Applications
UTF-8 is a widely used character encoding that is capable of encoding all possible characters, or code points, in Unicode. It's essential for supporting internationalization and enabling software to process languages like Chinese, Arabic, and Russian. Java applications generally handle UTF-8 seamlessly, but there occur situations where encoding issues emerge, especially in web contexts when dealing with form submissions, query parameters, and JSON payloads.
Issue in Spring Boot 2.2.0.RELEASE
With the Spring Boot 2.2.0.RELEASE, developers have reported that `MockMvc` no longer correctly handles UTF-8 encoded characters by default. This change primarily affects the way JSON payloads and request parameters are processed in unit tests. The root of this issue is a change in how character encoding is specified and handled within the framework, leading to defaults that may not align with previous configurations.
Example of the Problem
Consider the following test setup, where `MockMvc` is used to submit a JSON payload containing UTF-8 characters:
Related reading
- Mod in Java produces negative numbers
- Modify default JSON error response from Spring Boot Rest Controller
- MongoDB - No server chosen with java async driver and replica set
- MongoRepository findByThisAndThat custom Query with multiple parameters
- Mono class in Java what is, and when to use?
- Mono switchIfEmpty is always called
- More than one file was found with OS independent path 'META-INF/LICENSE
- More than one fragment with the name spring_web was found. This is not legal with relative ordering

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.