Affects: 6.2.0-SNAPSHOT

I was playing with the new MockMvcTest reusing some of my existing JsonUnit tests. When executing the test through mockMvc JSON content is serialized using UTF-8, when using MockMvcTest non-latin characters are garbled.

Maybe I missed some configuration, but I would expect the default behavior to be the same.

Comment From: snicoll

Thanks for the report. That's not the same setup. The former is using a WebApplicationContext. The latter is configuring things with only one controller (and without a context at all).

MockMvcTester isn't doing anything fancy, it just delegates to the MockMvc instance that you'd create yourself otherwise. From that perspective, the report is invalid, that is you'd get the same problem by creating a MockMvc instance with just a controller (via standaloneSetup).

Having said that, I'd like to investigate a bit more why the default character encoding is different in those two modes.

Comment From: lukas-krecan

If I remember correctly, the default for JSON in real controllers is UTF-8. But I agree, I should have formulated the report in a better way.

Comment From: snicoll

If I remember correctly, the default for JSON in real controllers is UTF-8.

No, MockMvc does default to ISO-8859-1 (per spec). I am not sure what you're experiencing to be honest. You can dump the exchange using andDo(MockMvcResultHandlers.print()) with MockMvc and with apply(MockMvcResultHandlers.print()) with MockMvcTester.

Give that a try and if you have UTF-8 in one and ISO-8859-1 in the other, please share an isolated sample that demonstrates that. You can attach a zip to this issue or push the code to a GitHub repository.

Comment From: lukas-krecan

Which spec are you talking about? For example here I see

JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

Comment From: snicoll

The servlet spec. This issue isn't about that anyway but about the inconsistency you claim. Can you please get back once you've tried what I requested?

Give that a try and if you have UTF-8 in one and ISO-8859-1 in the other, please share an isolated sample that demonstrates that. You can attach a zip to this issue or push the code to a GitHub repository.

Comment From: lukas-krecan

It's even more confusing then I thought. There are two issues.

The one I have reported is caused by this fix - JsonUnit is using UTF-8 when character encoding is not explicitely specified. When MockMvcTester is used, the conversion to string is done by Spring and ISO-8859-1 is used. So you may say that the issue is on JsonUnit side.

But, when I tried reproducing it, I started with a test based on RestTemplate and there UTF-8 is used (due to this). Test using MockMvc is using ISO-8859-1. So the inconsistency is between the RestTemplate based code that is assuming UTF-8 (and most likely the real controllers that produce UTF-8 encoded strings) for JSON and MockMvc.

I have already spend too much time on this, feel free to close it but I still think it's a bug in MockMvc. Your reproducer is here.

Comment From: snicoll

FTR, This was closed in the scope of the issue as reported (as the reproducer doesn't reproduce what was requested).

Comment From: lukas-krecan

Shall I report another ticket, something like "MockMVC defaults to ISO-8859-1 for JSON while the rest of the framework defaults to UTF-8"?

Comment From: snicoll

I have already spend too much time on this

It took me 15 minutes to build a sample that actually shows the problem: https://github.com/snicoll-scratches/spring-framework-32997. So it's neither standalone vs. context, neither MockMvcTester vs. MockMvc but rather what method you use to assert the json. As you found out yourself in 2019 already, special handling of JSON is required.

We're now in the right track with https://github.com/spring-projects/spring-framework/issues/33019 as the intent is to just use the character encoding of the response for everything and not put the onus on whoever is reading the body to force UTF-8 for json.