Skip to content

Commit d81365a

Browse files
Merge branch 'pr/562-sunil-integration-testing'
Conflicts: pom.xml
2 parents b7c563d + aed7a46 commit d81365a

7 files changed

Lines changed: 279 additions & 0 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
<version>${mockito.version}</version>
112112
<scope>test</scope>
113113
</dependency>
114+
<dependency>
115+
<groupId>com.jayway.jsonpath</groupId>
116+
<artifactId>json-path</artifactId>
117+
<version>2.2.0</version>
118+
</dependency>
114119
<dependency>
115120
<groupId>org.springframework</groupId>
116121
<artifactId>spring-test</artifactId>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.model;
2+
3+
public class Greeting {
4+
private int id;
5+
private String message;
6+
7+
public int getId() {
8+
return id;
9+
}
10+
11+
public void setId(int id) {
12+
this.id = id;
13+
}
14+
15+
public String getMessage() {
16+
return message;
17+
}
18+
19+
public void setMessage(String message) {
20+
this.message = message;
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring.web.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
11+
import org.springframework.web.servlet.view.JstlView;
12+
13+
@EnableWebMvc
14+
@Configuration
15+
@ComponentScan(basePackages = { "com.baeldung.web.controller" })
16+
public class ApplicationConfig extends WebMvcConfigurerAdapter {
17+
18+
public ApplicationConfig() {
19+
super();
20+
}
21+
22+
@Override
23+
public void addViewControllers(final ViewControllerRegistry registry) {
24+
super.addViewControllers(registry);
25+
registry.addViewController("/").setViewName("index");
26+
}
27+
28+
@Bean
29+
public ViewResolver viewResolver() {
30+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
31+
bean.setViewClass(JstlView.class);
32+
bean.setPrefix("/WEB-INF/jsp/");
33+
bean.setSuffix(".jsp");
34+
return bean;
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.web.controller;
2+
3+
import com.baeldung.model.Greeting;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
@Controller
8+
public class GreetController {
9+
10+
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
11+
public String index() {
12+
return "index";
13+
}
14+
15+
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
16+
@ResponseBody
17+
public Greeting greet() {
18+
Greeting greeting = new Greeting();
19+
greeting.setId(1);
20+
greeting.setMessage("Hello World!!!");
21+
return greeting;
22+
}
23+
24+
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
25+
@ResponseBody
26+
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
27+
Greeting greeting = new Greeting();
28+
greeting.setId(1);
29+
greeting.setMessage("Hello World " + name + "!!!");
30+
return greeting;
31+
}
32+
33+
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
34+
@ResponseBody
35+
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
36+
Greeting greeting = new Greeting();
37+
greeting.setId(1);
38+
greeting.setMessage("Hello World " + name + "!!!");
39+
return greeting;
40+
}
41+
42+
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
43+
@ResponseBody
44+
public Greeting greetWithPost() {
45+
Greeting greeting = new Greeting();
46+
greeting.setId(1);
47+
greeting.setMessage("Hello World!!!");
48+
return greeting;
49+
}
50+
51+
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
52+
@ResponseBody
53+
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
54+
Greeting greeting = new Greeting();
55+
greeting.setId(id);
56+
greeting.setMessage("Hello World " + name + "!!!");
57+
return greeting;
58+
}
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>Spring MVC - Integration Testing</h1>
4+
</body>
5+
</html>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.web.controller;
2+
3+
4+
import com.baeldung.spring.web.config.ApplicationConfig;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.mock.web.MockServletContext;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.MvcResult;
16+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
17+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
18+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
19+
import org.springframework.web.context.WebApplicationContext;
20+
21+
import javax.servlet.ServletContext;
22+
23+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
24+
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@WebAppConfiguration
27+
@ContextConfiguration(classes = {ApplicationConfig.class})
28+
public class GreetControllerIntegrationTest {
29+
30+
@Autowired
31+
private WebApplicationContext wac;
32+
33+
private MockMvc mockMvc;
34+
35+
private static final String CONTENT_TYPE = "application/json";
36+
37+
38+
@Before
39+
public void setup() throws Exception {
40+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
41+
}
42+
43+
@Test
44+
public void givenWAC_whenServletContext_thenItProvidesGreetController() {
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 givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
53+
this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index"));
54+
}
55+
56+
@Test
57+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
58+
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
59+
Assert.assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType());
60+
}
61+
62+
@Test
63+
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
64+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
65+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!"));
66+
}
67+
68+
@Test
69+
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
70+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
71+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World Doe!!!"));
72+
}
73+
74+
@Test
75+
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
76+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
77+
.andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
78+
}
79+
80+
@Test
81+
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
82+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
83+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
84+
}
85+
86+
@Test
87+
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
88+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
89+
.andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
90+
}
91+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.springframework.test.web.servlet.MockMvc;
6+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
7+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
8+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
9+
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
13+
14+
public class GreetControllerTest {
15+
16+
private MockMvc mockMvc;
17+
private static final String CONTENT_TYPE = "application/json";
18+
19+
@Before
20+
public void setup() {
21+
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
22+
}
23+
24+
@Test
25+
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
26+
this.mockMvc.perform(get("/homePage")).andExpect(view().name("index"));
27+
}
28+
29+
@Test
30+
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
31+
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!"));
32+
}
33+
34+
@Test
35+
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
36+
this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John!!!"));
37+
}
38+
39+
@Test
40+
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
41+
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
42+
}
43+
44+
@Test
45+
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
46+
this.mockMvc.perform(get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
47+
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
48+
}
49+
50+
@Test
51+
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
52+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE))
53+
.andExpect(jsonPath("$.message").value("Hello World!!!"));
54+
}
55+
56+
@Test
57+
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
58+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
59+
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
60+
}
61+
}

0 commit comments

Comments
 (0)