Skip to content

Commit 32aafb2

Browse files
committed
Set ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE in DefaultMockMvcBuilder
Issue: SPR-12553
1 parent c4049a9 commit 32aafb2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.test.web.servlet.setup;
1818

19+
import javax.servlet.ServletContext;
20+
1921
import org.springframework.util.Assert;
2022
import org.springframework.web.context.WebApplicationContext;
2123

@@ -25,6 +27,7 @@
2527
*
2628
* @author Rossen Stoyanchev
2729
* @author Rob Winch
30+
* @author Sebastien Deleuze
2831
* @since 3.2
2932
*/
3033
public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder<DefaultMockMvcBuilder> {
@@ -45,6 +48,8 @@ protected DefaultMockMvcBuilder(WebApplicationContext webAppContext) {
4548

4649
@Override
4750
protected WebApplicationContext initWebAppContext() {
51+
ServletContext servletContext = this.webAppContext.getServletContext();
52+
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
4853
return this.webAppContext;
4954
}
5055

spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -19,17 +19,21 @@
1919
import javax.servlet.http.HttpServletRequest;
2020
import javax.servlet.http.HttpServletResponse;
2121

22+
import static org.junit.Assert.assertEquals;
2223
import org.junit.Before;
2324
import org.junit.Test;
2425

26+
import org.springframework.mock.web.MockServletContext;
2527
import org.springframework.stereotype.Controller;
2628
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.context.support.WebApplicationContextUtils;
2730
import org.springframework.web.filter.OncePerRequestFilter;
2831

2932
/**
3033
* Tests for {@link DefaultMockMvcBuilder}.
3134
*
3235
* @author Rob Winch
36+
* @author Sebastien Deleuze
3337
*/
3438
public class DefaultMockMvcBuilderTests {
3539

@@ -60,6 +64,15 @@ public void addFilterPatternContainsNull() {
6064
builder.addFilter(new ContinueFilter(), (String) null);
6165
}
6266

67+
@Test // SPR-12553
68+
public void applicationContextAttribute() {
69+
MockServletContext servletContext = new MockServletContext();
70+
StubWebApplicationContext wac = new StubWebApplicationContext(servletContext);
71+
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(wac);
72+
assertEquals(builder.initWebAppContext(), WebApplicationContextUtils
73+
.getRequiredWebApplicationContext(servletContext));
74+
}
75+
6376

6477
@Controller
6578
private static class PersonController {

spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.stereotype.Controller;
2323
import org.springframework.web.bind.annotation.RequestMapping;
2424
import org.springframework.web.context.WebApplicationContext;
25+
import org.springframework.web.context.support.WebApplicationContextUtils;
2526
import org.springframework.web.method.HandlerMethod;
2627
import org.springframework.web.servlet.HandlerExecutionChain;
2728
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@@ -52,6 +53,16 @@ public void placeHoldersInRequestMapping() throws Exception {
5253
assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
5354
}
5455

56+
@Test // SPR-12553
57+
public void applicationContextAttribute() {
58+
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
59+
builder.addPlaceHolderValue("sys.login.ajax", "/foo");
60+
WebApplicationContext wac = builder.initWebAppContext();
61+
assertEquals(wac, WebApplicationContextUtils
62+
.getRequiredWebApplicationContext(wac.getServletContext()));
63+
}
64+
65+
5566

5667
@Controller
5768
private static class PlaceholderController {

0 commit comments

Comments
 (0)