2828@ ContextConfiguration (classes = { ApplicationConfig .class })
2929public class GreetControllerIntegrationTest {
3030
31- @ Autowired
32- private WebApplicationContext wac ;
33-
34- private MockMvc mockMvc ;
35-
36- @ Before
37- public void setup () throws Exception {
38- this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac )
39- // .alwaysExpect(MockMvcResultMatchers.status().isOk())
40- .build ();
41- }
42-
43- @ Test
44- public void verifyWac () {
45- ServletContext servletContext = wac .getServletContext ();
46- Assert .assertNotNull (servletContext );
47- Assert .assertTrue (servletContext instanceof MockServletContext );
48- Assert .assertNotNull (wac .getBean ("greetController" ));
49- }
50-
51- @ Test
52- public void verifyIndexJspViewName () throws Exception {
53- this .mockMvc
54- .perform (MockMvcRequestBuilders .get ("/homePage" ))
55- .andDo (print ())
56- .andExpect (MockMvcResultMatchers .view ().name ("index" ));
57- }
58-
59- @ Test
60- public void verifyGreet () throws Exception {
61- MvcResult mvcResult = this .mockMvc
62- .perform (MockMvcRequestBuilders .get ("/greet" ))
63- .andDo (print ())
64- .andExpect (MockMvcResultMatchers .status ().isOk ())
65- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World!!!" ))
66- .andReturn ();
67- Assert .assertEquals ("application/json;charset=UTF-8" , mvcResult .getResponse ().getContentType ());
68- }
69-
70- @ Test
71- public void verifyGreetWithPathVariable () throws Exception {
72- this .mockMvc .perform (MockMvcRequestBuilders .get ("/greetWithPathVariable/John" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ())
73- .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
74- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John!!!" ));
75- }
76-
77- @ Test
78- public void verifyGreetWithPathVariable_2 () throws Exception {
79- this .mockMvc
80- .perform (MockMvcRequestBuilders .get ("/greetWithPathVariable/{name}" , "Doe" ))
81- .andDo (print ())
82- .andExpect (MockMvcResultMatchers .status ().isOk ())
83- .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
84- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World Doe!!!" ));
85- }
86-
87- @ Test
88- public void verifyGreetWithQueryVariable () throws Exception {
89- this .mockMvc
90- .perform (MockMvcRequestBuilders .get ("/greetWithQueryVariable" ).param ("name" , "John Doe" ))
91- .andDo (print ())
92- .andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
93- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John Doe!!!" ));
94- }
95-
96- @ Test
97- public void verifyGreetWithPost () throws Exception {
98- this .mockMvc
99- .perform (MockMvcRequestBuilders .post ("/greetWithPost" ))
100- .andDo (print ())
101- .andExpect (MockMvcResultMatchers .status ().isOk ())
102- .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
103- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World!!!" ));
104- }
105-
106- @ Test
107- public void verifyGreetWithPostAndFormData () throws Exception {
108- this .mockMvc
109- .perform (MockMvcRequestBuilders .post ("/greetWithPostAndFormData" ).param ("id" , "1" ).param ("name" , "John Doe" ))
110- .andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ())
111- .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
112- .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John Doe!!!" )).andExpect (MockMvcResultMatchers .jsonPath ("$.id" ).value (1 ));
113- }
31+ @ Autowired
32+ private WebApplicationContext wac ;
33+
34+ private MockMvc mockMvc ;
35+
36+ @ Before
37+ public void setup () throws Exception {
38+ this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
39+ }
40+
41+ @ Test
42+ public void verifyWac () {
43+ ServletContext servletContext = wac .getServletContext ();
44+ Assert .assertNotNull (servletContext );
45+ Assert .assertTrue (servletContext instanceof MockServletContext );
46+ Assert .assertNotNull (wac .getBean ("greetController" ));
47+ }
48+
49+ @ Test
50+ public void verifyIndexJspViewName () throws Exception {
51+ this .mockMvc .perform (MockMvcRequestBuilders .get ("/homePage" )).andDo (print ()).andExpect (MockMvcResultMatchers .view ().name ("index" ));
52+ }
53+
54+ @ Test
55+ public void verifyGreet () throws Exception {
56+ MvcResult mvcResult = this .mockMvc .perform (MockMvcRequestBuilders .get ("/greet" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World!!!" )).andReturn ();
57+ Assert .assertEquals ("application/json;charset=UTF-8" , mvcResult .getResponse ().getContentType ());
58+ }
59+
60+ @ Test
61+ public void verifyGreetWithPathVariable () throws Exception {
62+ this .mockMvc .perform (MockMvcRequestBuilders .get ("/greetWithPathVariable/John" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
63+ .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John!!!" ));
64+ }
65+
66+ @ Test
67+ public void verifyGreetWithPathVariable_2 () throws Exception {
68+ this .mockMvc .perform (MockMvcRequestBuilders .get ("/greetWithPathVariable/{name}" , "Doe" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
69+ .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World Doe!!!" ));
70+ }
71+
72+ @ Test
73+ public void verifyGreetWithQueryVariable () throws Exception {
74+ this .mockMvc .perform (MockMvcRequestBuilders .get ("/greetWithQueryVariable" ).param ("name" , "John Doe" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ())
75+ .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" )).andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John Doe!!!" ));
76+ }
77+
78+ @ Test
79+ public void verifyGreetWithPost () throws Exception {
80+ this .mockMvc .perform (MockMvcRequestBuilders .post ("/greetWithPost" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" ))
81+ .andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World!!!" ));
82+ }
83+
84+ @ Test
85+ public void verifyGreetWithPostAndFormData () throws Exception {
86+ this .mockMvc .perform (MockMvcRequestBuilders .post ("/greetWithPostAndFormData" ).param ("id" , "1" ).param ("name" , "John Doe" )).andDo (print ()).andExpect (MockMvcResultMatchers .status ().isOk ())
87+ .andExpect (MockMvcResultMatchers .content ().contentType ("application/json;charset=UTF-8" )).andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("Hello World John Doe!!!" )).andExpect (MockMvcResultMatchers .jsonPath ("$.id" ).value (1 ));
88+ }
11489}
0 commit comments