如何使用Spring的MockMvc框架设置请求服务器名称?

如何使用Spring的MockMvc框架设置请求服务器名称?,spring,spring-mvc,servlets,junit,mockmvc,Spring,Spring Mvc,Servlets,Junit,Mockmvc,我使用的是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"

我使用的是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调用?

我们可以设置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;
        }
    }));
使用,我们可以设置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;
        }
    }));