Skip to content

Commit 34678f2

Browse files
Merge remote-tracking branch 'origin/pr/562' into pr/562-sunil-integration-testing
Conflicts: spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java
2 parents 4f9d950 + 709da6b commit 34678f2

8 files changed

Lines changed: 145 additions & 158 deletions

File tree

‎spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@EnableWebMvc
1414
@Configuration
15-
@ComponentScan(basePackages = {"com.baeldung.spring.controller"})
15+
@ComponentScan(basePackages = { "com.baeldung.spring.controller" })
1616
public class ApplicationConfig extends WebMvcConfigurerAdapter {
1717

1818
public ApplicationConfig() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.web;
2+
3+
import java.util.Set;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRegistration;
8+
9+
import org.springframework.web.WebApplicationInitializer;
10+
import org.springframework.web.context.ContextLoaderListener;
11+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.context.support.GenericWebApplicationContext;
13+
import org.springframework.web.servlet.DispatcherServlet;
14+
15+
public class WebAppInitializer implements WebApplicationInitializer {
16+
17+
@Override
18+
public void onStartup(final ServletContext sc) throws ServletException {
19+
20+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
21+
root.scan("com.baeldung.spring");
22+
23+
sc.addListener(new ContextLoaderListener(root));
24+
25+
final ServletRegistration.Dynamic appServlet = sc.addServlet("spring", new DispatcherServlet(new GenericWebApplicationContext()));
26+
appServlet.setLoadOnStartup(1);
27+
28+
final Set<String> mappingConflicts = appServlet.addMapping("/");
29+
if (!mappingConflicts.isEmpty()) {
30+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
31+
}
32+
}
33+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<html>
2-
<body>
3-
<h1>Spring MVC - Integration Testing</h1>
4-
</body>
2+
<body>
3+
<h1>Spring MVC - Integration Testing</h1>
4+
</body>
55
</html>

spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml renamed to spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd" >
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
55
</beans>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
4+
version="3.1">
5+
6+
<display-name>Spring MVC - Integration Testing</display-name>
7+
8+
<context-param>
9+
<param-name>contextClass</param-name>
10+
<param-value>
11+
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
12+
</param-value>
13+
</context-param>
14+
<context-param>
15+
<param-name>contextConfigLocation</param-name>
16+
<param-value>com.baeldung.spring</param-value>
17+
</context-param>
18+
19+
<listener>
20+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21+
</listener>
22+
23+
<servlet>
24+
<servlet-name>spring</servlet-name>
25+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
26+
<load-on-startup>1</load-on-startup>
27+
</servlet>
28+
29+
<servlet-mapping>
30+
<servlet-name>spring</servlet-name>
31+
<url-pattern>/</url-pattern>
32+
</servlet-mapping>
33+
</web-app>

‎spring-mvc-test/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java‎

Lines changed: 58 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -28,87 +28,62 @@
2828
@ContextConfiguration(classes = { ApplicationConfig.class })
2929
public 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 givenWAC_whenServletContext_thenItProvidesGreetController() {
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 givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
51+
this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index"));
52+
}
53+
54+
@Test
55+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() 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 givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() 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 givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() 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 givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() 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 givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() 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 givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() 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
}

‎spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java‎

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,40 @@ public void setup() {
2424
}
2525

2626
@Test
27-
public void verifyIndexJspViewName() throws Exception {
28-
this.mockMvc.perform(get("/homePage"))
29-
.andExpect(view().name("index"));
27+
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
28+
this.mockMvc.perform(get("/homePage")).andExpect(view().name("index"));
3029
}
3130

3231
@Test
33-
public void verifyGreet() throws Exception {
34-
this.mockMvc.perform(get("/greet"))
35-
.andExpect(status().isOk())
36-
.andExpect(content().contentType("application/json;charset=UTF-8"))
37-
.andExpect(jsonPath("$.message").value("Hello World!!!"));
32+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
33+
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World!!!"));
3834
}
3935

4036
@Test
41-
public void verifyGreetWithPathVariable() throws Exception {
42-
this.mockMvc.perform(get("/greetWithPathVariable/John"))
43-
.andExpect(status().isOk())
44-
.andExpect(content().contentType("application/json;charset=UTF-8"))
45-
.andExpect(jsonPath("$.message").value("Hello World John!!!"));
37+
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
38+
this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World John!!!"));
4639
}
4740

4841
@Test
49-
public void verifyGreetWithPathVariable_2() throws Exception {
50-
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe"))
51-
.andExpect(status().isOk())
52-
.andExpect(content().contentType("application/json;charset=UTF-8"))
53-
.andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
42+
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
43+
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
5444
}
5545

5646
@Test
57-
public void verifyGreetWithQueryVariable() throws Exception {
58-
this.mockMvc
59-
.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe"))
60-
.andDo(print())
61-
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
62-
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
47+
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
48+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
49+
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
6350
}
6451

6552
@Test
66-
public void verifyGreetWithPost() throws Exception {
67-
this.mockMvc
68-
.perform(MockMvcRequestBuilders.post("/greetWithPost"))
69-
.andDo(print())
70-
.andExpect(MockMvcResultMatchers.status().isOk())
71-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
53+
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
54+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
7255
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
7356
}
7457

7558
@Test
76-
public void verifyGreetWithPostAndFormData() throws Exception {
77-
this.mockMvc
78-
.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe"))
79-
.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
80-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
81-
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
59+
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
60+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
61+
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
8262
}
8363
}

0 commit comments

Comments
 (0)