Skip to content

Commit eefd1c4

Browse files
committed
Add context hierarchy tests to Spring MVC Test
Issue: SPR-5613
1 parent 4c5d771 commit eefd1c4

File tree

7 files changed

+158
-10
lines changed

7 files changed

+158
-10
lines changed

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java

+45-5
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616

1717
package org.springframework.test.web.servlet.samples.context;
1818

19-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
21-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22-
2319
import org.junit.Before;
2420
import org.junit.Test;
2521
import org.junit.runner.RunWith;
22+
import org.mockito.Mockito;
2623
import org.springframework.beans.factory.annotation.Autowired;
2724
import org.springframework.context.annotation.Bean;
2825
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.http.MediaType;
2927
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.ContextHierarchy;
3029
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3130
import org.springframework.test.context.web.WebAppConfiguration;
31+
import org.springframework.test.web.Person;
3232
import org.springframework.test.web.servlet.MockMvc;
33+
import org.springframework.test.web.servlet.samples.context.JavaConfigTests.RootConfig;
3334
import org.springframework.test.web.servlet.samples.context.JavaConfigTests.WebConfig;
3435
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3536
import org.springframework.web.context.WebApplicationContext;
@@ -42,6 +43,13 @@
4243
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
4344
import org.springframework.web.servlet.view.tiles3.TilesView;
4445

46+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
47+
48+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
49+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
50+
import static org.mockito.Mockito.*;
51+
52+
4553
/**
4654
* Tests with Java configuration.
4755
*
@@ -50,18 +58,33 @@
5058
*/
5159
@RunWith(SpringJUnit4ClassRunner.class)
5260
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
53-
@ContextConfiguration(classes = WebConfig.class)
61+
@ContextHierarchy({
62+
@ContextConfiguration(classes = RootConfig.class),
63+
@ContextConfiguration(classes = WebConfig.class)
64+
})
5465
public class JavaConfigTests {
5566

5667
@Autowired
5768
private WebApplicationContext wac;
5869

70+
@Autowired
71+
private PersonDao personDao;
72+
5973
private MockMvc mockMvc;
6074

6175

6276
@Before
6377
public void setup() {
6478
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
79+
when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe"));
80+
}
81+
82+
@Test
83+
public void person() throws Exception {
84+
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
85+
.andDo(print())
86+
.andExpect(status().isOk())
87+
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
6588
}
6689

6790
@Test
@@ -72,10 +95,27 @@ public void tilesDefinitions() throws Exception {
7295
}
7396

7497

98+
@Configuration
99+
static class RootConfig {
100+
101+
@Bean
102+
public PersonDao personDao() {
103+
return Mockito.mock(PersonDao.class);
104+
}
105+
}
106+
75107
@Configuration
76108
@EnableWebMvc
77109
static class WebConfig extends WebMvcConfigurerAdapter {
78110

111+
@Autowired
112+
private RootConfig rootConfig;
113+
114+
@Bean
115+
public PersonController personController() {
116+
return new PersonController(this.rootConfig.personDao());
117+
}
118+
79119
@Override
80120
public void addResourceHandlers(ResourceHandlerRegistry registry) {
81121
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet.samples.context;
18+
19+
import org.springframework.stereotype.Controller;
20+
import org.springframework.test.web.Person;
21+
import org.springframework.web.bind.annotation.PathVariable;
22+
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.bind.annotation.RequestMethod;
24+
import org.springframework.web.bind.annotation.ResponseBody;
25+
26+
@Controller
27+
public class PersonController {
28+
29+
private final PersonDao personDao;
30+
31+
32+
public PersonController(PersonDao personDao) {
33+
this.personDao = personDao;
34+
}
35+
36+
@RequestMapping(value="/person/{id}", method=RequestMethod.GET)
37+
@ResponseBody
38+
public Person getPerson(@PathVariable long id) {
39+
return this.personDao.getPerson(id);
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet.samples.context;
18+
19+
import org.springframework.test.web.Person;
20+
21+
public interface PersonDao {
22+
23+
Person getPerson(Long id);
24+
25+
}

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.runner.RunWith;
2929
import org.springframework.beans.factory.annotation.Autowired;
3030
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.ContextHierarchy;
3132
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3233
import org.springframework.test.context.web.WebAppConfiguration;
3334
import org.springframework.test.web.servlet.MockMvc;
@@ -42,7 +43,10 @@
4243
*/
4344
@RunWith(SpringJUnit4ClassRunner.class)
4445
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
45-
@ContextConfiguration("servlet-context.xml")
46+
@ContextHierarchy({
47+
@ContextConfiguration("root-context.xml"),
48+
@ContextConfiguration("servlet-context.xml")
49+
})
4650
public class WebAppResourceTests {
4751

4852
@Autowired

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@
1616

1717
package org.springframework.test.web.servlet.samples.context;
1818

19-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
20-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
21-
2219
import org.junit.Before;
2320
import org.junit.Test;
2421
import org.junit.runner.RunWith;
2522
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.http.MediaType;
2624
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.ContextHierarchy;
2726
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2827
import org.springframework.test.context.web.WebAppConfiguration;
28+
import org.springframework.test.web.Person;
2929
import org.springframework.test.web.servlet.MockMvc;
3030
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3131
import org.springframework.web.context.WebApplicationContext;
3232

33+
import static org.mockito.Mockito.*;
34+
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
38+
3339
/**
3440
* Tests with XML configuration.
3541
*
@@ -38,18 +44,33 @@
3844
*/
3945
@RunWith(SpringJUnit4ClassRunner.class)
4046
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
41-
@ContextConfiguration("servlet-context.xml")
47+
@ContextHierarchy({
48+
@ContextConfiguration("root-context.xml"),
49+
@ContextConfiguration("servlet-context.xml")
50+
})
4251
public class XmlConfigTests {
4352

4453
@Autowired
4554
private WebApplicationContext wac;
4655

56+
@Autowired
57+
private PersonDao personDao;
58+
4759
private MockMvc mockMvc;
4860

4961

5062
@Before
5163
public void setup() {
5264
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
65+
when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe"));
66+
}
67+
68+
@Test
69+
public void person() throws Exception {
70+
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
71+
.andDo(print())
72+
.andExpect(status().isOk())
73+
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
5374
}
5475

5576
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7+
8+
<bean id="personDao" class="org.mockito.Mockito" factory-method="mock">
9+
<constructor-arg value="org.springframework.test.web.servlet.samples.context.PersonDao" />
10+
</bean>
11+
12+
</beans>

spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
<mvc:annotation-driven />
99

10+
<bean class="org.springframework.test.web.servlet.samples.context.PersonController">
11+
<constructor-arg ref="personDao"/>
12+
</bean>
13+
1014
<mvc:view-controller path="/" view-name="home" />
1115

1216
<mvc:resources mapping="/resources/**" location="/resources/" />

0 commit comments

Comments
 (0)