Skip to content

Commit

Permalink
Set ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE in DefaultMockMvcBuilder
Browse files Browse the repository at this point in the history
Issue: SPR-12553
  • Loading branch information
sdeleuze committed Dec 22, 2014
1 parent c4049a9 commit 32aafb2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +27,7 @@
*
* @author Rossen Stoyanchev
* @author Rob Winch
* @author Sebastien Deleuze
* @since 3.2
*/
public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder<DefaultMockMvcBuilder> {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 32aafb2

Please sign in to comment.