Set ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE in DefaultMockMvcBuilder

Issue: SPR-12553
This commit is contained in:
Sebastien Deleuze 2014-12-22 11:53:45 +01:00
parent c4049a989a
commit 32aafb21ff
3 changed files with 30 additions and 1 deletions

View File

@ -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 @@ import org.springframework.web.context.WebApplicationContext;
*
* @author Rossen Stoyanchev
* @author Rob Winch
* @author Sebastien Deleuze
* @since 3.2
*/
public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder<DefaultMockMvcBuilder> {
@ -45,6 +48,8 @@ public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder<DefaultMockMvc
@Override
protected WebApplicationContext initWebAppContext() {
ServletContext servletContext = this.webAppContext.getServletContext();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
return this.webAppContext;
}

View File

@ -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.ServletException;
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 class DefaultMockMvcBuilderTests {
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 {

View File

@ -22,6 +22,7 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
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 class StandaloneMockMvcBuilderTests {
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 {