Skip to content

Commit c996f8c

Browse files
Sunil GulabaniSunil Gulabani
authored andcommitted
Updated formatting.
1 parent 6fb2305 commit c996f8c

8 files changed

Lines changed: 205 additions & 248 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
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

18-
public ApplicationConfig() {
19-
super();
20-
}
21-
22-
@Override
18+
public ApplicationConfig() {
19+
super();
20+
}
21+
22+
@Override
2323
public void addViewControllers(final ViewControllerRegistry registry) {
2424
super.addViewControllers(registry);
2525
registry.addViewController("/").setViewName("index");
2626
}
2727

2828
@Bean
2929
public ViewResolver viewResolver() {
30-
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
30+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
3131
bean.setViewClass(JstlView.class);
3232
bean.setPrefix("/WEB-INF/jsp/");
3333
bean.setSuffix(".jsp");
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.baeldung.spring.bean;
22

33
public class Greeting {
4-
private int id;
5-
private String message;
6-
7-
public int getId() {
8-
return id;
9-
}
10-
public void setId(int id) {
11-
this.id = id;
12-
}
13-
public String getMessage() {
14-
return message;
15-
}
16-
public void setMessage(String message) {
17-
this.message = message;
18-
}
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+
}
1922
}

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

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,54 @@
1111

1212
@Controller
1313
public class GreetController {
14-
15-
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
16-
public String index() {
17-
return "index";
18-
}
19-
20-
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
21-
@ResponseBody
22-
public Greeting greet() {
23-
Greeting greeting = new Greeting();
24-
greeting.setId(1);
25-
greeting.setMessage("Hello World!!!");
26-
return greeting;
27-
}
28-
29-
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
30-
@ResponseBody
31-
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
32-
Greeting greeting = new Greeting();
33-
greeting.setId(1);
34-
greeting.setMessage("Hello World " + name + "!!!");
35-
return greeting;
36-
}
37-
38-
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
39-
@ResponseBody
40-
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
41-
Greeting greeting = new Greeting();
42-
greeting.setId(1);
43-
greeting.setMessage("Hello World " + name + "!!!");
44-
return greeting;
45-
}
46-
47-
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
48-
@ResponseBody
49-
public Greeting greetWithPost() {
50-
Greeting greeting = new Greeting();
51-
greeting.setId(1);
52-
greeting.setMessage("Hello World!!!");
53-
return greeting;
54-
}
5514

56-
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
57-
@ResponseBody
58-
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
59-
Greeting greeting = new Greeting();
60-
greeting.setId(id);
61-
greeting.setMessage("Hello World " + name + "!!!");
62-
return greeting;
63-
}
15+
@RequestMapping(value = "/homePage", method = RequestMethod.GET)
16+
public String index() {
17+
return "index";
18+
}
19+
20+
@RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json")
21+
@ResponseBody
22+
public Greeting greet() {
23+
Greeting greeting = new Greeting();
24+
greeting.setId(1);
25+
greeting.setMessage("Hello World!!!");
26+
return greeting;
27+
}
28+
29+
@RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json")
30+
@ResponseBody
31+
public Greeting greetWithPathVariable(@PathVariable("name") String name) {
32+
Greeting greeting = new Greeting();
33+
greeting.setId(1);
34+
greeting.setMessage("Hello World " + name + "!!!");
35+
return greeting;
36+
}
37+
38+
@RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json")
39+
@ResponseBody
40+
public Greeting greetWithQueryVariable(@RequestParam("name") String name) {
41+
Greeting greeting = new Greeting();
42+
greeting.setId(1);
43+
greeting.setMessage("Hello World " + name + "!!!");
44+
return greeting;
45+
}
46+
47+
@RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json")
48+
@ResponseBody
49+
public Greeting greetWithPost() {
50+
Greeting greeting = new Greeting();
51+
greeting.setId(1);
52+
greeting.setMessage("Hello World!!!");
53+
return greeting;
54+
}
55+
56+
@RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json")
57+
@ResponseBody
58+
public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) {
59+
Greeting greeting = new Greeting();
60+
greeting.setId(id);
61+
greeting.setMessage("Hello World " + name + "!!!");
62+
return greeting;
63+
}
6464
}
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>
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: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5-
version="3.1">
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">
65

7-
<display-name>Spring MVC - Integration Testing</display-name>
6+
<display-name>Spring MVC - Integration Testing</display-name>
87

9-
<context-param>
10-
<param-name>contextClass</param-name>
11-
<param-value>
8+
<context-param>
9+
<param-name>contextClass</param-name>
10+
<param-value>
1211
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
1312
</param-value>
14-
</context-param>
15-
<context-param>
16-
<param-name>contextConfigLocation</param-name>
17-
<param-value>com.baeldung.spring</param-value>
18-
</context-param>
19-
20-
<listener>
21-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22-
</listener>
13+
</context-param>
14+
<context-param>
15+
<param-name>contextConfigLocation</param-name>
16+
<param-value>com.baeldung.spring</param-value>
17+
</context-param>
2318

24-
<servlet>
25-
<servlet-name>spring</servlet-name>
26-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
27-
<load-on-startup>1</load-on-startup>
28-
</servlet>
29-
30-
<servlet-mapping>
31-
<servlet-name>spring</servlet-name>
32-
<url-pattern>/</url-pattern>
33-
</servlet-mapping>
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>
3433
</web-app>

‎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 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

Comments
 (0)