diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java b/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java index c437dd568a..09be5ee113 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java @@ -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() { diff --git a/spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java b/spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java new file mode 100644 index 0000000000..23fad058d0 --- /dev/null +++ b/spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java @@ -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 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"); + } + } +} diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp b/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp index 89c7ca6c81..2cf02bc2d8 100644 --- a/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp @@ -1,5 +1,5 @@ - -

Spring MVC - Integration Testing

- + +

Spring MVC - Integration Testing

+ \ No newline at end of file diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml b/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml similarity index 51% rename from spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml rename to spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml index 40718ab3a4..2b8192e742 100644 --- a/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml +++ b/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> \ No newline at end of file diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml b/spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml new file mode 100644 index 0000000000..dc0233a7fc --- /dev/null +++ b/spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml @@ -0,0 +1,33 @@ + + + + Spring MVC - Integration Testing + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + com.baeldung.spring + + + + org.springframework.web.context.ContextLoaderListener + + + + spring + org.springframework.web.servlet.DispatcherServlet + 1 + + + + spring + / + + \ No newline at end of file diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/web.xml b/spring-mvc-test/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 8cf7a9a37b..0000000000 --- a/spring-mvc-test/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - Spring MVC - Integration Testing - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - com.baeldung.spring - - - - org.springframework.web.context.ContextLoaderListener - - - - spring - org.springframework.web.servlet.DispatcherServlet - 1 - - - - spring - / - - \ No newline at end of file diff --git a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java index d7d697dda9..abed0a977e 100644 --- a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java +++ b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java @@ -28,87 +28,62 @@ import javax.servlet.ServletContext; @ContextConfiguration(classes = { ApplicationConfig.class }) public class GreetControllerIntegrationTest { - @Autowired - private WebApplicationContext wac; + @Autowired + private WebApplicationContext wac; - private MockMvc mockMvc; + private MockMvc mockMvc; - @Before - public void setup() throws Exception { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) -// .alwaysExpect(MockMvcResultMatchers.status().isOk()) - .build(); - } + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } - @Test - public void verifyWac() { - ServletContext servletContext = wac.getServletContext(); - Assert.assertNotNull(servletContext); - Assert.assertTrue(servletContext instanceof MockServletContext); - Assert.assertNotNull(wac.getBean("greetController")); - } + @Test + public void givenWAC_whenServletContext_thenItProvidesGreetController() { + ServletContext servletContext = wac.getServletContext(); + Assert.assertNotNull(servletContext); + Assert.assertTrue(servletContext instanceof MockServletContext); + Assert.assertNotNull(wac.getBean("greetController")); + } - @Test - public void verifyIndexJspViewName() throws Exception { - this.mockMvc - .perform(MockMvcRequestBuilders.get("/homePage")) - .andDo(print()) - .andExpect(MockMvcResultMatchers.view().name("index")); - } + @Test + public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception { + 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(); - Assert.assertEquals("application/json;charset=UTF-8", mvcResult.getResponse().getContentType()); - } + @Test + public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception { + 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")) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!")); - } + @Test + public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception { + 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")) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World Doe!!!")); - } + @Test + public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() 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")) + .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!!!")); - } + @Test + public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() 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!!!")); + } - @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")) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")); - } + @Test + public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception { + 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)); - } + @Test + public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() 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)); + } } \ No newline at end of file diff --git a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java index b53aba75c0..8e624544cd 100644 --- a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java +++ b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java @@ -24,60 +24,40 @@ public class GreetControllerTest { } @Test - public void verifyIndexJspViewName() throws Exception { - this.mockMvc.perform(get("/homePage")) - .andExpect(view().name("index")); + public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception { + 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!!!")); + public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception { + 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!!!")); + public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() 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!!!")); } @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!!!")); + public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() 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!!!")); } @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!!!")); + public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() 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!!!")); } @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")) + public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception { + 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)); + public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() 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)); } }