org.springframework.test.web.servlet.MockMvc.getDispatcherServlet()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(3.1k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中org.springframework.test.web.servlet.MockMvc.getDispatcherServlet()方法的一些代码示例,展示了MockMvc.getDispatcherServlet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockMvc.getDispatcherServlet()方法的具体详情如下:
包路径:org.springframework.test.web.servlet.MockMvc
类名称:MockMvc
方法名:getDispatcherServlet

MockMvc.getDispatcherServlet介绍

[英]Return the underlying DispatcherServlet instance that this MockMvc was initialized with.

This is intended for use in custom request processing scenario where a request handling component happens to delegate to the DispatcherServletat runtime and therefore needs to be injected with it.

For most processing scenarios, simply use MockMvc#perform, or if you need to configure the DispatcherServlet, provide a DispatcherServletCustomizer to the MockMvcBuilder.
[中]返回初始化此MockMvc的基础DispatcherServlet实例。
这是为了在自定义请求处理场景中使用,在该场景中,请求处理组件恰好在运行时委托给DispatcherServlet,因此需要注入它。
对于大多数处理场景,只需使用MockMvc#perform,或者如果需要配置DispatcherServlet,请为MockMvcBuilder提供DispatcherServletCustomizer。

代码示例

代码示例来源:origin: spring-projects/spring-security

@Test
public void requestWhenCreateSessionIsSetToIfRequiredThenCreatesSessionOnLoginChallenge()
    throws Exception {
  this.spring.configLocations(this.xml("CreateSessionIfRequired")).autowire();
  ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
  MockHttpServletRequest request = get("/auth").buildRequest(servletContext);
  MockHttpServletResponse response = request(request, this.spring.getContext());
  assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_MOVED_TEMPORARILY);
  assertThat(request.getSession(false)).isNotNull();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void requestWhenCreateSessionIsSetToIfRequiredThenDoesNotCreateSessionOnPublicInvocation()
    throws Exception {
  this.spring.configLocations(this.xml("CreateSessionIfRequired")).autowire();
  ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
  MockHttpServletRequest request = get("/").buildRequest(servletContext);
  MockHttpServletResponse response = request(request, this.spring.getContext());
  assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
  assertThat(request.getSession(false)).isNull();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void requestWhenCreateSessionIsSetToIfRequiredThenCreatesSessionOnLogin()
    throws Exception {
  this.spring.configLocations(this.xml("CreateSessionIfRequired")).autowire();
  ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
  MockHttpServletRequest request = post("/login")
      .param("username", "user")
      .param("password", "password")
      .buildRequest(servletContext);
  request = csrf().postProcessRequest(request);
  MockHttpServletResponse response = request(request, this.spring.getContext());
  assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_MOVED_TEMPORARILY);
  assertThat(request.getSession(false)).isNotNull();
}

代码示例来源:origin: org.springframework.boot/spring-boot-test-autoconfigure

@Bean
@ConditionalOnMissingBean
public DispatcherServlet dispatcherServlet(MockMvc mockMvc) {
  return mockMvc.getDispatcherServlet();
}

相关文章