diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
index fb75d1048f6c258b664f48cd59da4102d7385ae1..63ee9376bf9fe8e440463fa834e94673f5f4a9b0 100644
--- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
@@ -16,20 +16,21 @@
package org.springframework.test.web.servlet.samples.context;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.samples.context.JavaConfigTests.RootConfig;
import org.springframework.test.web.servlet.samples.context.JavaConfigTests.WebConfig;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@@ -42,6 +43,13 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+import static org.mockito.Mockito.*;
+
+
/**
* Tests with Java configuration.
*
@@ -50,18 +58,33 @@ import org.springframework.web.servlet.view.tiles3.TilesView;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
-@ContextConfiguration(classes = WebConfig.class)
+@ContextHierarchy({
+ @ContextConfiguration(classes = RootConfig.class),
+ @ContextConfiguration(classes = WebConfig.class)
+})
public class JavaConfigTests {
@Autowired
private WebApplicationContext wac;
+ @Autowired
+ private PersonDao personDao;
+
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
+ when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe"));
+ }
+
+ @Test
+ public void person() throws Exception {
+ this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
+ .andDo(print())
+ .andExpect(status().isOk())
+ .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
@Test
@@ -72,10 +95,27 @@ public class JavaConfigTests {
}
+ @Configuration
+ static class RootConfig {
+
+ @Bean
+ public PersonDao personDao() {
+ return Mockito.mock(PersonDao.class);
+ }
+ }
+
@Configuration
@EnableWebMvc
static class WebConfig extends WebMvcConfigurerAdapter {
+ @Autowired
+ private RootConfig rootConfig;
+
+ @Bean
+ public PersonController personController() {
+ return new PersonController(this.rootConfig.personDao());
+ }
+
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java
new file mode 100644
index 0000000000000000000000000000000000000000..ffeea88e449f4703c41dc1ebc2a49e478f3dce89
--- /dev/null
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2002-2013 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.test.web.servlet.samples.context;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.test.web.Person;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+@Controller
+public class PersonController {
+
+ private final PersonDao personDao;
+
+
+ public PersonController(PersonDao personDao) {
+ this.personDao = personDao;
+ }
+
+ @RequestMapping(value="/person/{id}", method=RequestMethod.GET)
+ @ResponseBody
+ public Person getPerson(@PathVariable long id) {
+ return this.personDao.getPerson(id);
+ }
+
+}
diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java
new file mode 100644
index 0000000000000000000000000000000000000000..035b9595d4c4a2f0cf27347f6d2eb947b81e91b4
--- /dev/null
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2002-2013 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.test.web.servlet.samples.context;
+
+import org.springframework.test.web.Person;
+
+public interface PersonDao {
+
+ Person getPerson(Long id);
+
+}
diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java
index 2fb361f9154e87a734ce53b4d488aa1d1360e1f3..23cd46d5c34f401e24e90f7dfa98d265b2243d26 100644
--- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java
@@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
@@ -42,7 +43,10 @@ import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
-@ContextConfiguration("servlet-context.xml")
+@ContextHierarchy({
+ @ContextConfiguration("root-context.xml"),
+ @ContextConfiguration("servlet-context.xml")
+})
public class WebAppResourceTests {
@Autowired
diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java
index 73ab1ab0a95cc766a60c08e25216ca5e252b9376..e9091ef5b08de40a0c1f01b5dfa837312965b239 100644
--- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java
@@ -16,20 +16,26 @@
package org.springframework.test.web.servlet.samples.context;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
-
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
+import static org.mockito.Mockito.*;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
/**
* Tests with XML configuration.
*
@@ -38,18 +44,33 @@ import org.springframework.web.context.WebApplicationContext;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
-@ContextConfiguration("servlet-context.xml")
+@ContextHierarchy({
+ @ContextConfiguration("root-context.xml"),
+ @ContextConfiguration("servlet-context.xml")
+})
public class XmlConfigTests {
@Autowired
private WebApplicationContext wac;
+ @Autowired
+ private PersonDao personDao;
+
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
+ when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe"));
+ }
+
+ @Test
+ public void person() throws Exception {
+ this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
+ .andDo(print())
+ .andExpect(status().isOk())
+ .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
@Test
diff --git a/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml b/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml
new file mode 100644
index 0000000000000000000000000000000000000000..6456a634caebba746774f2542f8b1f1c774310e5
--- /dev/null
+++ b/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml b/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
index 4e567b1a843d9186ca2ee3f35ff90d32a42f551c..615534d26d6cf362a3809f9990485e493f7d09c2 100644
--- a/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
+++ b/spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
@@ -7,6 +7,10 @@
+
+
+
+