Merge remote-tracking branch 'origin/pr/562' into pr/562-sunil-integration-testing
Conflicts: spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java
This commit is contained in:
commit
34678f2077
@ -12,7 +12,7 @@ import org.springframework.web.servlet.view.JstlView;
|
|||||||
|
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = {"com.baeldung.spring.controller"})
|
@ComponentScan(basePackages = { "com.baeldung.spring.controller" })
|
||||||
public class ApplicationConfig extends WebMvcConfigurerAdapter {
|
public class ApplicationConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
public ApplicationConfig() {
|
public ApplicationConfig() {
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.web;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
public class WebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartup(final ServletContext sc) throws ServletException {
|
||||||
|
|
||||||
|
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||||
|
root.scan("com.baeldung.spring");
|
||||||
|
|
||||||
|
sc.addListener(new ContextLoaderListener(root));
|
||||||
|
|
||||||
|
final ServletRegistration.Dynamic appServlet = sc.addServlet("spring", new DispatcherServlet(new GenericWebApplicationContext()));
|
||||||
|
appServlet.setLoadOnStartup(1);
|
||||||
|
|
||||||
|
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
||||||
|
if (!mappingConflicts.isEmpty()) {
|
||||||
|
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<h1>Spring MVC - Integration Testing</h1>
|
<h1>Spring MVC - Integration Testing</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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>
|
</beans>
|
33
spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml
Normal file
33
spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?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"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||||
|
version="3.1">
|
||||||
|
|
||||||
|
<display-name>Spring MVC - Integration Testing</display-name>
|
||||||
|
|
||||||
|
<context-param>
|
||||||
|
<param-name>contextClass</param-name>
|
||||||
|
<param-value>
|
||||||
|
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||||
|
</param-value>
|
||||||
|
</context-param>
|
||||||
|
<context-param>
|
||||||
|
<param-name>contextConfigLocation</param-name>
|
||||||
|
<param-value>com.baeldung.spring</param-value>
|
||||||
|
</context-param>
|
||||||
|
|
||||||
|
<listener>
|
||||||
|
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||||
|
</listener>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>spring</servlet-name>
|
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>spring</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
@ -1,34 +0,0 @@
|
|||||||
<?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"
|
|
||||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
|
||||||
version="3.1">
|
|
||||||
|
|
||||||
<display-name>Spring MVC - Integration Testing</display-name>
|
|
||||||
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextClass</param-name>
|
|
||||||
<param-value>
|
|
||||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
|
||||||
</param-value>
|
|
||||||
</context-param>
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextConfigLocation</param-name>
|
|
||||||
<param-value>com.baeldung.spring</param-value>
|
|
||||||
</context-param>
|
|
||||||
|
|
||||||
<listener>
|
|
||||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
|
||||||
</listener>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>spring</servlet-name>
|
|
||||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>spring</servlet-name>
|
|
||||||
<url-pattern>/</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
</web-app>
|
|
@ -28,87 +28,62 @@ import javax.servlet.ServletContext;
|
|||||||
@ContextConfiguration(classes = { ApplicationConfig.class })
|
@ContextConfiguration(classes = { ApplicationConfig.class })
|
||||||
public class GreetControllerIntegrationTest {
|
public class GreetControllerIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws Exception {
|
public void setup() throws Exception {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||||
// .alwaysExpect(MockMvcResultMatchers.status().isOk())
|
}
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyWac() {
|
public void givenWAC_whenServletContext_thenItProvidesGreetController() {
|
||||||
ServletContext servletContext = wac.getServletContext();
|
ServletContext servletContext = wac.getServletContext();
|
||||||
Assert.assertNotNull(servletContext);
|
Assert.assertNotNull(servletContext);
|
||||||
Assert.assertTrue(servletContext instanceof MockServletContext);
|
Assert.assertTrue(servletContext instanceof MockServletContext);
|
||||||
Assert.assertNotNull(wac.getBean("greetController"));
|
Assert.assertNotNull(wac.getBean("greetController"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyIndexJspViewName() throws Exception {
|
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index"));
|
||||||
.perform(MockMvcRequestBuilders.get("/homePage"))
|
}
|
||||||
.andDo(print())
|
|
||||||
.andExpect(MockMvcResultMatchers.view().name("index"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreet() throws Exception {
|
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
MvcResult mvcResult = this.mockMvc
|
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
|
||||||
.perform(MockMvcRequestBuilders.get("/greet"))
|
Assert.assertEquals("application/json;charset=UTF-8", mvcResult.getResponse().getContentType());
|
||||||
.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
|
@Test
|
||||||
public void verifyGreetWithPathVariable() throws Exception {
|
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
||||||
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!"));
|
||||||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithPathVariable_2() throws Exception {
|
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
||||||
.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe"))
|
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World 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
|
@Test
|
||||||
public void verifyGreetWithQueryVariable() throws Exception {
|
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe"))
|
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World 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
|
@Test
|
||||||
public void verifyGreetWithPost() throws Exception {
|
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
||||||
.perform(MockMvcRequestBuilders.post("/greetWithPost"))
|
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
|
||||||
.andDo(print())
|
}
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
|
||||||
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
|
||||||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithPostAndFormData() throws Exception {
|
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe"))
|
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
|
||||||
.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));
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -24,60 +24,40 @@ public class GreetControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyIndexJspViewName() throws Exception {
|
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
|
||||||
this.mockMvc.perform(get("/homePage"))
|
this.mockMvc.perform(get("/homePage")).andExpect(view().name("index"));
|
||||||
.andExpect(view().name("index"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreet() throws Exception {
|
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(get("/greet"))
|
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World!!!"));
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
|
||||||
.andExpect(jsonPath("$.message").value("Hello World!!!"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithPathVariable() throws Exception {
|
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(get("/greetWithPathVariable/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!!!"));
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
|
||||||
.andExpect(jsonPath("$.message").value("Hello World John!!!"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithPathVariable_2() throws Exception {
|
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "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!!!"));
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
|
||||||
.andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithQueryVariable() throws Exception {
|
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe"))
|
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World 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
|
@Test
|
||||||
public void verifyGreetWithPost() throws Exception {
|
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
|
||||||
.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!!!"));
|
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyGreetWithPostAndFormData() throws Exception {
|
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe"))
|
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
|
||||||
.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