Spring MockMVC inject mockHttpServletRequest when not in method signature

后端 未结 1 1497
走了就别回头了
走了就别回头了 2021-01-01 18:48

Given I have inherited some Spring MVC controller code with signature

@RequestMapping(value = \"/upload\", method = RequestMethod.POST)
public ModelAndView u         


        
相关标签:
1条回答
  • 2021-01-01 19:03

    You can add a RequestPostProcessor. Which you can then pass in to the mockmvc stuff by using the with() method.

    mockMvc().perform(
      fileUpload("/upload")
      .file(FileFactory.stringContent("myFile"))
      .with(new RequestPostProcessor() { 
        public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
           request.setRemoteAddr("12345"); 
           return request;
        }}));
    

    Something like that should work.

    0 讨论(0)
提交回复
热议问题