mockMvc
.perform(MockMvcRequestBuilders
.post("/mock/post")
.content(JSON.toJSONString(person1))
.contentType(MediaType.APPLICATION_JSON)
.header("X-Access-token", "1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().setCharacterEncoding("UTF-8");
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = {"code":200,"result":"å
许è¿å
¥"}
Forwarded URL = null
Redirected URL = null
Cookies = []
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 03 Feb 2021 03:13:42 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 200,
"result": "允许进入"
}
不知道怎么办了。。求助
1
uselessVisitor OP |
2
Jrue0011 2021-02-04 10:04:07 +08:00
我是直接参考 MockMvcResultHandlers.PrintWriterPrintingResultHandler 写了一个直接指定 Charset 的 PrintingResultHandler 子类然后在测试里用,而不用 MockMvcResultHandlers.print()...
https://paste.ubuntu.com/p/dmsjmqWm5W/ |
3
Jrue0011 2021-02-04 10:16:56 +08:00
不过 MockHttpServletRequest 和 MockHttpServletResponse 设置 CharacterEncoding 的时候会更新 ContentType...
所以如果有调用 andExpect 对比 ContentType 的话,比如 andExpect(content().contentType(MediaType.APPLICATION_JSON)) 得放在 andDo 前面... |