From eefd1c4ca69fc82c63230dfe30a5752c661388e9 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 7 Mar 2013 11:20:19 -0500 Subject: [PATCH] Add context hierarchy tests to Spring MVC Test Issue: SPR-5613 --- .../samples/context/JavaConfigTests.java | 50 +++++++++++++++++-- .../samples/context/PersonController.java | 42 ++++++++++++++++ .../servlet/samples/context/PersonDao.java | 25 ++++++++++ .../samples/context/WebAppResourceTests.java | 6 ++- .../samples/context/XmlConfigTests.java | 29 +++++++++-- .../servlet/samples/context/root-context.xml | 12 +++++ .../samples/context/servlet-context.xml | 4 ++ 7 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java create mode 100644 spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml 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 fb75d1048f..63ee9376bf 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 0000000000..ffeea88e44 --- /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 0000000000..035b9595d4 --- /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 2fb361f915..23cd46d5c3 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 73ab1ab0a9..e9091ef5b0 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 0000000000..6456a634ca --- /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 4e567b1a84..615534d26d 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 @@ + + + + -- GitLab