多年后返回Java / Spring.尝试开发控制器测试.我想测试一个OPTIONS请求的处理程序,但是当我使用MockMvc做类似的事情时
MockHttpServletRequestBuilder optionsRequest = options("/endpoint/1234");
optionsRequest.header("Origin", "http://blah");
this.mockMvc.perform(optionsRequest).
andExpect(status().isOk()).
andExpect(header().string("Access-Control-Allow-Credentials", "application/json")).
andExpect(header().string("Access-Control-Allow-Origin", "application/json")).
andExpect(header().string("Access-Control-Allow-Methods", "application/json")).
andExpect(header().string("Access-Control-Allow-Headers", "application/json"));
设置
this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
如果controller是我的控制器的实例,则选项处理程序永远不会被解雇.
在我的服务器启动中,我们需要这样做
dispatcherServlet.setDispatchOptionsRequest(true);
获取正在运行的应用程序中处理的请求.
在测试设置中是否可以访问调度程序servlet,以便可以设置相同的选项?
我已经退回到设置模拟请求/响应对象并暂时手动调用方法了.