Skip to content

Commit 4f9d950

Browse files
BAEL-175 - Integrtion testing minor changes
1 parent 6fb2305 commit 4f9d950

4 files changed

Lines changed: 136 additions & 133 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
@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
}

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

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,69 @@
1515
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
1616

1717
public class GreetControllerTest {
18-
19-
private MockMvc mockMvc;
20-
21-
@Before
22-
public void setup() {
23-
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
24-
}
25-
26-
@Test
27-
public void verifyIndexJspViewName() throws Exception {
28-
this.mockMvc.perform(get("/homePage"))
29-
.andExpect(view().name("index"));
30-
}
31-
32-
@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!!!"));
38-
}
39-
40-
@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!!!"));
46-
}
47-
48-
@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!!!"));
54-
}
55-
56-
@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!!!"));
63-
}
6418

65-
@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"))
72-
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
73-
}
19+
private MockMvc mockMvc;
7420

75-
@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));
82-
}
21+
@Before
22+
public void setup() {
23+
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
24+
}
25+
26+
@Test
27+
public void verifyIndexJspViewName() throws Exception {
28+
this.mockMvc.perform(get("/homePage"))
29+
.andExpect(view().name("index"));
30+
}
31+
32+
@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!!!"));
38+
}
39+
40+
@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!!!"));
46+
}
47+
48+
@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!!!"));
54+
}
55+
56+
@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!!!"));
63+
}
64+
65+
@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"))
72+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
73+
}
74+
75+
@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));
82+
}
8383
}

0 commit comments

Comments
 (0)