Testing Spring MVC Router with MockMVC

ぐ巨炮叔叔 提交于 2019-12-05 02:51:32

After doing some digging, I've discovered that the HTTPRequestAdapter.parseRequest() method has an issue with the way that MockMVC sends requests. Specifically, the request sent by MockMVC doesn't include a header in the request with the name host.

HTTPRequestAdapter requires that header and does not account for the fact that it can be null, so it generates the NullPointerException.

I fixed the issue with this code:

mockMvc.perform(get("/validation-success")
       .header("host", "localhost:80"))
       .andExpect(status().isOk());

The host header won't be null and your tests should pass.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!