我使用的是Spring 4.3.8.RELEASE。在我的集成测试中,我使用SPring的MockMvc框架,设置如此...
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
...
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
...
mockMvc.perform(get(contextPath + "/path")
.contextPath(contextPath)
.principal(auth)
.param("param1", param1)
.param("param2", param2))
我无法弄清楚如何设置我的请求的服务器名称。也就是说,当调用我的控制器填充
时final HttpServletRequest request
如何设置
request.getServerName()
来自MockMvc电话的?
答案 0 :(得分:4)
使用RequestPostProcessor,我们可以设置MockHttpServletRequest
并模拟数据。
mockMvc.perform(get(contextPath + "/path").contextPath(contextPath).principal(auth).param("param1", param1).param("param2", param2).with(new RequestPostProcessor() {
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
request.setServerName("system");
return request;
}
}));