Updated formatting.
This commit is contained in:
		
							parent
							
								
									6fb230546d
								
							
						
					
					
						commit
						c996f8ccd5
					
				| @ -12,7 +12,7 @@ import org.springframework.web.servlet.view.JstlView; | ||||
| 
 | ||||
| @EnableWebMvc | ||||
| @Configuration | ||||
| @ComponentScan(basePackages = {"com.baeldung.spring.controller"}) | ||||
| @ComponentScan(basePackages = { "com.baeldung.spring.controller" }) | ||||
| public class ApplicationConfig extends WebMvcConfigurerAdapter { | ||||
| 
 | ||||
|     public ApplicationConfig() { | ||||
|  | ||||
| @ -7,12 +7,15 @@ public class Greeting { | ||||
|     public int getId() { | ||||
|         return id; | ||||
|     } | ||||
| 
 | ||||
|     public void setId(int id) { | ||||
|         this.id = id; | ||||
|     } | ||||
| 
 | ||||
|     public String getMessage() { | ||||
|         return message; | ||||
|     } | ||||
| 
 | ||||
|     public void setMessage(String message) { | ||||
|         this.message = message; | ||||
|     } | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| <html> | ||||
| 	<body> | ||||
| <body> | ||||
| 	<h1>Spring MVC - Integration Testing</h1> | ||||
| 	</body> | ||||
| </body> | ||||
| </html> | ||||
| @ -1,5 +1,5 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||||
| 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd" > | ||||
| 	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> | ||||
| </beans> | ||||
| @ -1,6 +1,5 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
| <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
| 	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | ||||
| 	version="3.1"> | ||||
| 
 | ||||
|  | ||||
| @ -35,9 +35,7 @@ public class GreetControllerIntegrationTest { | ||||
| 
 | ||||
|     @Before | ||||
|     public void setup() throws Exception { | ||||
| 		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) | ||||
| //				.alwaysExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.build(); | ||||
|         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -50,65 +48,42 @@ public class GreetControllerIntegrationTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyIndexJspViewName() throws Exception { | ||||
| 		this.mockMvc | ||||
| 					.perform(MockMvcRequestBuilders.get("/homePage")) | ||||
| 					.andDo(print()) | ||||
| 					.andExpect(MockMvcResultMatchers.view().name("index")); | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreet() throws Exception { | ||||
| 		MvcResult mvcResult = this.mockMvc | ||||
| 										.perform(MockMvcRequestBuilders.get("/greet")) | ||||
| 										.andDo(print()) | ||||
| 										.andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 										.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")) | ||||
| 										.andReturn(); | ||||
|         MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn(); | ||||
|         Assert.assertEquals("application/json;charset=UTF-8", mvcResult.getResponse().getContentType()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPathVariable() throws Exception { | ||||
| 		this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|                 .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPathVariable_2() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")) | ||||
| 				.andDo(print()) | ||||
| 				.andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|                 .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World Doe!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithQueryVariable() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")) | ||||
| 				.andDo(print()) | ||||
| 				.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
|                 .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPost() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.post("/greetWithPost")) | ||||
| 				.andDo(print()) | ||||
| 				.andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|                 .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPostAndFormData() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")) | ||||
| 				.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1)); | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
|                 .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1)); | ||||
|     } | ||||
| } | ||||
| @ -25,59 +25,39 @@ public class GreetControllerTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyIndexJspViewName() throws Exception { | ||||
| 		this.mockMvc.perform(get("/homePage")) | ||||
| 				.andExpect(view().name("index")); | ||||
|         this.mockMvc.perform(get("/homePage")).andExpect(view().name("index")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreet() throws Exception { | ||||
| 		this.mockMvc.perform(get("/greet")) | ||||
| 				.andExpect(status().isOk()) | ||||
| 				.andExpect(content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(jsonPath("$.message").value("Hello World!!!")); | ||||
|         this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPathVariable() throws Exception { | ||||
| 		this.mockMvc.perform(get("/greetWithPathVariable/John")) | ||||
| 				.andExpect(status().isOk()) | ||||
| 				.andExpect(content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(jsonPath("$.message").value("Hello World John!!!")); | ||||
|         this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World John!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPathVariable_2() throws Exception { | ||||
| 		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!!!")); | ||||
|         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!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithQueryVariable() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")) | ||||
| 				.andDo(print()) | ||||
| 				.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
|                 .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPost() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.post("/greetWithPost")) | ||||
| 				.andDo(print()) | ||||
| 				.andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
|                 .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void verifyGreetWithPostAndFormData() throws Exception { | ||||
| 		this.mockMvc | ||||
| 				.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")) | ||||
| 				.andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
| 				.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) | ||||
| 				.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1)); | ||||
|         this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()) | ||||
|                 .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1)); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user