|
15 | 15 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
16 | 16 |
|
17 | 17 | 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 | | - } |
64 | 18 |
|
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; |
74 | 20 |
|
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 | + } |
83 | 83 | } |
0 commit comments