Spring boot测试类MockMvcResultHandlers.print()乱码

现象

Spring boot升级到2.3以后,测试类中的MockMvcResultHandlers.print()打印出来的字符都是乱码了,查了一下改动比较大,一个一个修改很麻烦,可以抽象到BaseTest中

基类中的公共方法

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 测试post接口
*/
public void postForTest(String except, Object content, String url, boolean isPrint) throws Exception {
ResultActions resultActions = this.mock.perform(
post(url).headers(headers).content(new JsonMapper().toJson(content))
).andExpect(status().isOk());
resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
if (isPrint) {
resultActions.andDo(MockMvcResultHandlers.print());
}
resultActions.andExpect(content().json(except));
}
文章目录
  1. 1. 现象
  2. 2. 基类中的公共方法