diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java index f123b8311ad9..455b53a36abf 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java @@ -16,6 +16,8 @@ package org.springframework.test.web.servlet.setup; +import javax.servlet.ServletContext; + import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; @@ -25,6 +27,7 @@ * * @author Rossen Stoyanchev * @author Rob Winch + * @author Sebastien Deleuze * @since 3.2 */ public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder { @@ -45,6 +48,8 @@ protected DefaultMockMvcBuilder(WebApplicationContext webAppContext) { @Override protected WebApplicationContext initWebAppContext() { + ServletContext servletContext = this.webAppContext.getServletContext(); + servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext); return this.webAppContext; } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java index 61824d6bd59c..c08c0a4c0c84 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at @@ -19,17 +19,21 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; +import org.springframework.mock.web.MockServletContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.filter.OncePerRequestFilter; /** * Tests for {@link DefaultMockMvcBuilder}. * * @author Rob Winch + * @author Sebastien Deleuze */ public class DefaultMockMvcBuilderTests { @@ -60,6 +64,15 @@ public void addFilterPatternContainsNull() { builder.addFilter(new ContinueFilter(), (String) null); } + @Test // SPR-12553 + public void applicationContextAttribute() { + MockServletContext servletContext = new MockServletContext(); + StubWebApplicationContext wac = new StubWebApplicationContext(servletContext); + DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(wac); + assertEquals(builder.initWebAppContext(), WebApplicationContextUtils + .getRequiredWebApplicationContext(servletContext)); + } + @Controller private static class PersonController { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java index 1a6ebb562929..c0e18d3ab1d6 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java @@ -22,6 +22,7 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @@ -52,6 +53,16 @@ public void placeHoldersInRequestMapping() throws Exception { assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName()); } + @Test // SPR-12553 + public void applicationContextAttribute() { + TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController()); + builder.addPlaceHolderValue("sys.login.ajax", "/foo"); + WebApplicationContext wac = builder.initWebAppContext(); + assertEquals(wac, WebApplicationContextUtils + .getRequiredWebApplicationContext(wac.getServletContext())); + } + + @Controller private static class PlaceholderController {