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

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

本文整理了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

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

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

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

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

  1. @Test
  2. public void requestWhenCreateSessionIsSetToIfRequiredThenCreatesSessionOnLogin()
  3. throws Exception {
  4. this.spring.configLocations(this.xml("CreateSessionIfRequired")).autowire();
  5. ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
  6. MockHttpServletRequest request = post("/login")
  7. .param("username", "user")
  8. .param("password", "password")
  9. .buildRequest(servletContext);
  10. request = csrf().postProcessRequest(request);
  11. MockHttpServletResponse response = request(request, this.spring.getContext());
  12. assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_MOVED_TEMPORARILY);
  13. assertThat(request.getSession(false)).isNotNull();
  14. }

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

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public DispatcherServlet dispatcherServlet(MockMvc mockMvc) {
  4. return mockMvc.getDispatcherServlet();
  5. }

相关文章