16
16
17
17
package org .springframework .test .web .servlet .samples .context ;
18
18
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
-
23
19
import org .junit .Before ;
24
20
import org .junit .Test ;
25
21
import org .junit .runner .RunWith ;
22
+ import org .mockito .Mockito ;
26
23
import org .springframework .beans .factory .annotation .Autowired ;
27
24
import org .springframework .context .annotation .Bean ;
28
25
import org .springframework .context .annotation .Configuration ;
26
+ import org .springframework .http .MediaType ;
29
27
import org .springframework .test .context .ContextConfiguration ;
28
+ import org .springframework .test .context .ContextHierarchy ;
30
29
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
31
30
import org .springframework .test .context .web .WebAppConfiguration ;
31
+ import org .springframework .test .web .Person ;
32
32
import org .springframework .test .web .servlet .MockMvc ;
33
+ import org .springframework .test .web .servlet .samples .context .JavaConfigTests .RootConfig ;
33
34
import org .springframework .test .web .servlet .samples .context .JavaConfigTests .WebConfig ;
34
35
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
35
36
import org .springframework .web .context .WebApplicationContext ;
42
43
import org .springframework .web .servlet .view .tiles3 .TilesConfigurer ;
43
44
import org .springframework .web .servlet .view .tiles3 .TilesView ;
44
45
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
+
45
53
/**
46
54
* Tests with Java configuration.
47
55
*
50
58
*/
51
59
@ RunWith (SpringJUnit4ClassRunner .class )
52
60
@ 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
+ })
54
65
public class JavaConfigTests {
55
66
56
67
@ Autowired
57
68
private WebApplicationContext wac ;
58
69
70
+ @ Autowired
71
+ private PersonDao personDao ;
72
+
59
73
private MockMvc mockMvc ;
60
74
61
75
62
76
@ Before
63
77
public void setup () {
64
78
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}" ));
65
88
}
66
89
67
90
@ Test
@@ -72,10 +95,27 @@ public void tilesDefinitions() throws Exception {
72
95
}
73
96
74
97
98
+ @ Configuration
99
+ static class RootConfig {
100
+
101
+ @ Bean
102
+ public PersonDao personDao () {
103
+ return Mockito .mock (PersonDao .class );
104
+ }
105
+ }
106
+
75
107
@ Configuration
76
108
@ EnableWebMvc
77
109
static class WebConfig extends WebMvcConfigurerAdapter {
78
110
111
+ @ Autowired
112
+ private RootConfig rootConfig ;
113
+
114
+ @ Bean
115
+ public PersonController personController () {
116
+ return new PersonController (this .rootConfig .personDao ());
117
+ }
118
+
79
119
@ Override
80
120
public void addResourceHandlers (ResourceHandlerRegistry registry ) {
81
121
registry .addResourceHandler ("/resources/**" ).addResourceLocations ("/resources/" );
0 commit comments