@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; }
@Test public void webAppContextSetupWithNullServletContext() { exception.expect(IllegalArgumentException.class); exception.expectMessage(equalTo("WebApplicationContext must have a ServletContext")); webAppContextSetup(new StubWebApplicationContext(null)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySet() { StubWebApplicationContext child = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, child); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySet() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); WebApplicationContext wac = builder.initWebAppContext(); assertSame(root, wac); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySetWithContextHierarchy() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, root); StaticWebApplicationContext child = new StaticWebApplicationContext(); child.setParent(root); child.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext().getParent(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
/** * See /SPR-14277 */ @Test public void dispatcherServletCustomizer() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id")); builder.dispatchOptions(true); MockMvc mvc = builder.build(); DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc) .getPropertyValue("servlet"); assertEquals("test-id", ds.getContextId()); }
@Test public void dispatcherServletCustomizerProcessedInOrder() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id")); builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id")); builder.dispatchOptions(true); MockMvc mvc = builder.build(); DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc) .getPropertyValue("servlet"); assertEquals("override-id", ds.getContextId()); }
@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; }
@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; }