/** * Create a new MockServletConfig. * @param servletContext the ServletContext that the servlet runs in * @param servletName the name of the servlet */ public MockServletConfig(@Nullable ServletContext servletContext, String servletName) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.servletName = servletName; }
/** * Create a new MockFilterConfig. * @param servletContext the ServletContext that the servlet runs in * @param filterName the name of the filter */ public MockFilterConfig(@Nullable ServletContext servletContext, String filterName) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.filterName = filterName; }
/** * Create a new MockHttpSession. * @param servletContext the ServletContext that the session runs in * @param id a unique identifier for this session */ public MockHttpSession(@Nullable ServletContext servletContext, @Nullable String id) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.id = (id != null ? id : Integer.toString(nextId++)); }
/** * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}, * {@code method}, and {@code requestURI}. * <p>The preferred locale will be set to {@link Locale#ENGLISH}. * @param servletContext the ServletContext that the request runs in (may be * {@code null} to use a default {@link MockServletContext}) * @param method the request method (may be {@code null}) * @param requestURI the request URI (may be {@code null}) * @see #setMethod * @see #setRequestURI * @see #setPreferredLocales * @see MockServletContext */ public MockHttpServletRequest(@Nullable ServletContext servletContext, @Nullable String method, @Nullable String requestURI) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.method = method; this.requestURI = requestURI; this.locales.add(Locale.ENGLISH); }
/** * Create new MockServletConfig. * @param servletContext the ServletContext that the JSP page runs in * @param request the current HttpServletRequest * @param response the current HttpServletResponse * @param servletConfig the ServletConfig (hardly ever accessed from within a tag) */ public MockPageContext(@Nullable ServletContext servletContext, @Nullable HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable ServletConfig servletConfig) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.request = (request != null ? request : new MockHttpServletRequest(servletContext)); this.response = (response != null ? response : new MockHttpServletResponse()); this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext)); }
@Override protected WebApplicationContext initWebAppContext() { MockServletContext servletContext = new MockServletContext(); StubWebApplicationContext wac = new StubWebApplicationContext(servletContext); registerMvcSingletons(wac); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); return wac; }
private MockServletContext mockServletContext(String servletPath) { MockServletContext servletContext = spy(new MockServletContext()); final ServletRegistration registration = mock(ServletRegistration.class); when(registration.getMappings()).thenReturn(Collections.singleton(servletPath)); Answer<Map<String, ? extends ServletRegistration>> answer = invocation -> Collections.singletonMap("spring", registration); when(servletContext.getServletRegistrations()).thenAnswer(answer); return servletContext; }
@Before public void setup() { this.servletContext = spy(new MockServletContext()); this.request = new MockHttpServletRequest("GET", ""); this.request.setMethod("GET"); this.response = new MockHttpServletResponse(); this.chain = new MockFilterChain(); }
@Test public void registerContextAndGetContext() { MockServletContext sc2 = new MockServletContext(); sc.setContextPath("/"); sc.registerContext("/second", sc2); assertSame(sc, sc.getContext("/")); assertSame(sc2, sc.getContext("/second")); }
@Test public void test() { MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/"); parent.characterEncoding("UTF-8"); Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent); assertNotNull(result); assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass()); MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result; MockHttpServletRequest request = builder.buildRequest(new MockServletContext()); assertEquals("UTF-8", request.getCharacterEncoding()); }
private void loadConfig(Class<?>... configs) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setAllowCircularReferences(false); context.register(configs); context.setServletContext(new MockServletContext()); context.refresh(); } }
public void loadConfig(Class<?>... configs) { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(configs); this.context.setServletContext(new MockServletContext()); this.context.refresh(); this.context.getAutowireCapableBeanFactory().autowireBean(this); }
public void loadConfig(Class<?>... configs) { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(configs); this.context.setServletContext(new MockServletContext()); this.context.refresh(); this.context.getAutowireCapableBeanFactory().autowireBean(this); } }
public void loadConfig(Class<?>... configs) { this.context = new AnnotationConfigWebApplicationContext(); this.context.register(configs); this.context.setServletContext(new MockServletContext()); this.context.refresh(); this.context.getAutowireCapableBeanFactory().autowireBean(this); } }
@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
@Test public void shouldDelegateToTheGivenServlet() throws IOException, ServletException { MockServletContext ctx = new MockServletContext(); ctx.addInitParameter(DelegatingListener.DELEGATE_SERVLET, DummyServlet.class.getCanonicalName()); ServletContextEvent evt = new ServletContextEvent(ctx); DelegatingListener listener = new DelegatingListener(); listener.contextInitialized(evt); assertThat((DummyServlet) ctx.getAttribute(DelegatingListener.DELEGATE_SERVLET), isA(DummyServlet.class)); DelegatingServlet servlet = new DelegatingServlet(); servlet.init(new MockServletConfig(ctx)); servlet.service(httpServletRequest, new MockHttpServletResponse()); verify(servletRequestWrapper).setRequestURI("/go/stuff/action"); } }
@Test public void registerServletParamPropertySources_AbstractRefreshableWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value");
@Test public void registerServletParamPropertySources_StaticWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value");
@Before public void setUp() { ServletContext servletContext = new MockServletContext(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext); mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK); RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse())); this.wac.setServletContext(servletContext); new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class); this.wac.refresh(); this.mockMvc = webAppContextSetup(this.wac) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }
public void autowire() { this.context.setServletContext(new MockServletContext()); this.context.setServletConfig(new MockServletConfig()); this.context.refresh(); if (this.context.containsBean(SPRING_SECURITY_FILTER_CHAIN)) { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .apply(new AddFilter()).build(); this.context.getBeanFactory() .registerResolvableDependency(MockMvc.class, mockMvc); } AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(this.context.getBeanFactory()); bpp.processInjection(this.test); }