From 6fb230546db624ebf906486d32fe5117691cacef Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Mon, 1 Aug 2016 16:44:57 +0530 Subject: [PATCH 1/5] Integration Testing with Spring MVC. --- pom.xml | 1 + spring-mvc-test/README | 5 + spring-mvc-test/pom.xml | 193 ++++++++++++++++++ .../baeldung/spring/ApplicationConfig.java | 36 ++++ .../com/baeldung/spring/bean/Greeting.java | 19 ++ .../spring/controller/GreetController.java | 64 ++++++ .../src/main/resources/logback.xml | 18 ++ .../src/main/webapp/WEB-INF/jsp/index.jsp | 5 + .../main/webapp/WEB-INF/spring-servlet.xml | 5 + .../src/main/webapp/WEB-INF/web.xml | 34 +++ .../GreetControllerIntegrationTest.java | 114 +++++++++++ .../controller/GreetControllerTest.java | 83 ++++++++ 12 files changed, 577 insertions(+) create mode 100644 spring-mvc-test/README create mode 100644 spring-mvc-test/pom.xml create mode 100644 spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java create mode 100644 spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java create mode 100644 spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java create mode 100644 spring-mvc-test/src/main/resources/logback.xml create mode 100644 spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp create mode 100644 spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml create mode 100644 spring-mvc-test/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java create mode 100644 spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java diff --git a/pom.xml b/pom.xml index 62d3d03633..c32ee82ce1 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,7 @@ mutation-testing spring-mvc-velocity xstream + spring-mvc-test diff --git a/spring-mvc-test/README b/spring-mvc-test/README new file mode 100644 index 0000000000..9f4a0a60d4 --- /dev/null +++ b/spring-mvc-test/README @@ -0,0 +1,5 @@ +To compile and run the project, execute following command: + + mvn clean install org.codehaus.cargo:cargo-maven2-plugin:run + +URL: http://localhost:8080/spring-mvc-test/ diff --git a/spring-mvc-test/pom.xml b/spring-mvc-test/pom.xml new file mode 100644 index 0000000000..3c4875f087 --- /dev/null +++ b/spring-mvc-test/pom.xml @@ -0,0 +1,193 @@ + + 4.0.0 + com.baeldung + spring-mvc-test + 0.1-SNAPSHOT + spring-mvc-test + war + + + + 4.3.1.RELEASE + + 1.7.21 + 1.1.7 + + 3.4 + + 1.3 + 4.12 + 1.10.19 + 4.4.5 + 4.5.2 + + + 3.5.1 + 2.6 + 2.19.1 + 2.19.1 + 3.0.1 + + + 1.5.0 + + + + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + org.springframework + spring-websocket + ${org.springframework.version} + + + + com.fasterxml.jackson.core + jackson-core + 2.7.3 + + + com.fasterxml.jackson.core + jackson-databind + 2.7.3 + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + javax.servlet + jstl + 1.2 + runtime + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${org.slf4j.version} + + + + junit + junit + ${junit.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.jsonpath + json-path + 2.2.0 + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + + spring-mvc-test + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + maven-resources-plugin + 2.7 + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + **/*IntegrationTest.java + + + + + + integration-test + verify + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + + jetty8x + embedded + + + + 8080 + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..1a5b590854 --- /dev/null +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java @@ -0,0 +1,36 @@ +package com.baeldung.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = {"com.baeldung.spring.controller"}) +public class ApplicationConfig extends WebMvcConfigurerAdapter { + + public ApplicationConfig() { + super(); + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/").setViewName("index"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/jsp/"); + bean.setSuffix(".jsp"); + return bean; + } +} \ No newline at end of file diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java new file mode 100644 index 0000000000..d7ddaf2fd1 --- /dev/null +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.bean; + +public class Greeting { + private int id; + private String message; + + 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; + } +} diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java new file mode 100644 index 0000000000..0f62df2a71 --- /dev/null +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java @@ -0,0 +1,64 @@ +package com.baeldung.spring.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.spring.bean.Greeting; + +@Controller +public class GreetController { + + @RequestMapping(value = "/homePage", method = RequestMethod.GET) + public String index() { + return "index"; + } + + @RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greet() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithPathVariable(@PathVariable("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithQueryVariable(@RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPost() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(id); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } +} \ No newline at end of file diff --git a/spring-mvc-test/src/main/resources/logback.xml b/spring-mvc-test/src/main/resources/logback.xml new file mode 100644 index 0000000000..166c369905 --- /dev/null +++ b/spring-mvc-test/src/main/resources/logback.xml @@ -0,0 +1,18 @@ + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..89c7ca6c81 --- /dev/null +++ b/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp @@ -0,0 +1,5 @@ + + +

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.xml new file mode 100644 index 0000000000..40718ab3a4 --- /dev/null +++ b/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet.xml @@ -0,0 +1,5 @@ + + + \ 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 new file mode 100644 index 0000000000..8cf7a9a37b --- /dev/null +++ b/spring-mvc-test/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,34 @@ + + + + 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 new file mode 100644 index 0000000000..d7d697dda9 --- /dev/null +++ b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java @@ -0,0 +1,114 @@ +package com.baeldung.spring.controller; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.spring.ApplicationConfig; + +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import javax.servlet.ServletContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { ApplicationConfig.class }) +public class GreetControllerIntegrationTest { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) +// .alwaysExpect(MockMvcResultMatchers.status().isOk()) + .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 verifyIndexJspViewName() 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 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 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 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 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 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)); + } +} \ 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 new file mode 100644 index 0000000000..155b6b4a50 --- /dev/null +++ b/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java @@ -0,0 +1,83 @@ +package com.baeldung.spring.controller; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +public class GreetControllerTest { + + private MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build(); + } + + @Test + public void verifyIndexJspViewName() 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!!!")); + } + + @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!!!")); + } + + @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!!!")); + } + + @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 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 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)); + } +} From 4f9d9502e24481d254781cd4336c3f1aeb371606 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Tue, 2 Aug 2016 06:33:49 +0200 Subject: [PATCH 2/5] BAEL-175 - Integrtion testing minor changes --- .../baeldung/spring/ApplicationConfig.java | 12 +- .../com/baeldung/spring/bean/Greeting.java | 33 ++--- .../spring/controller/GreetController.java | 98 +++++++------- .../controller/GreetControllerTest.java | 126 +++++++++--------- 4 files changed, 136 insertions(+), 133 deletions(-) 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 1a5b590854..c437dd568a 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 @@ -15,11 +15,11 @@ import org.springframework.web.servlet.view.JstlView; @ComponentScan(basePackages = {"com.baeldung.spring.controller"}) public class ApplicationConfig extends WebMvcConfigurerAdapter { - public ApplicationConfig() { - super(); - } - - @Override + public ApplicationConfig() { + super(); + } + + @Override public void addViewControllers(final ViewControllerRegistry registry) { super.addViewControllers(registry); registry.addViewController("/").setViewName("index"); @@ -27,7 +27,7 @@ public class ApplicationConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/jsp/"); bean.setSuffix(".jsp"); diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java index d7ddaf2fd1..11c0a79b0e 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java @@ -1,19 +1,22 @@ package com.baeldung.spring.bean; public class Greeting { - private int id; - private String message; - - 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; - } + private int id; + private String message; + + 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; + } } diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java index 0f62df2a71..d563f80918 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java @@ -11,54 +11,54 @@ import com.baeldung.spring.bean.Greeting; @Controller public class GreetController { - - @RequestMapping(value = "/homePage", method = RequestMethod.GET) - public String index() { - return "index"; - } - - @RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greet() { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greetWithPathVariable(@PathVariable("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greetWithQueryVariable(@RequestParam("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public Greeting greetWithPost() { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World!!!"); - return greeting; - } - @RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(id); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } + @RequestMapping(value = "/homePage", method = RequestMethod.GET) + public String index() { + return "index"; + } + + @RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greet() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithPathVariable(@PathVariable("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithQueryVariable(@RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPost() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(id); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } } \ 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 155b6b4a50..b53aba75c0 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 @@ -15,69 +15,69 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; public class GreetControllerTest { - - private MockMvc mockMvc; - - @Before - public void setup() { - this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build(); - } - - @Test - public void verifyIndexJspViewName() 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!!!")); - } - - @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!!!")); - } - - @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!!!")); - } - - @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 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!!!")); - } + private MockMvc mockMvc; - @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)); - } + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build(); + } + + @Test + public void verifyIndexJspViewName() 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!!!")); + } + + @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!!!")); + } + + @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!!!")); + } + + @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 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 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)); + } } From c996f8ccd5d45f5e6e1292ac9b08ba6f7f39eff5 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Wed, 3 Aug 2016 15:04:36 +0530 Subject: [PATCH 3/5] Updated formatting. --- .../baeldung/spring/ApplicationConfig.java | 14 +- .../com/baeldung/spring/bean/Greeting.java | 33 ++--- .../spring/controller/GreetController.java | 98 +++++++------- .../src/main/webapp/WEB-INF/jsp/index.jsp | 6 +- .../main/webapp/WEB-INF/spring-servlet.xml | 2 +- .../src/main/webapp/WEB-INF/web.xml | 53 ++++---- .../GreetControllerIntegrationTest.java | 121 +++++++----------- .../controller/GreetControllerTest.java | 106 +++++++-------- 8 files changed, 195 insertions(+), 238 deletions(-) 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 1a5b590854..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,14 +12,14 @@ 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() { - super(); - } - - @Override + public ApplicationConfig() { + super(); + } + + @Override public void addViewControllers(final ViewControllerRegistry registry) { super.addViewControllers(registry); registry.addViewController("/").setViewName("index"); @@ -27,7 +27,7 @@ public class ApplicationConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/jsp/"); bean.setSuffix(".jsp"); diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java index d7ddaf2fd1..11c0a79b0e 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java @@ -1,19 +1,22 @@ package com.baeldung.spring.bean; public class Greeting { - private int id; - private String message; - - 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; - } + private int id; + private String message; + + 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; + } } diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java index 0f62df2a71..d563f80918 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java +++ b/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java @@ -11,54 +11,54 @@ import com.baeldung.spring.bean.Greeting; @Controller public class GreetController { - - @RequestMapping(value = "/homePage", method = RequestMethod.GET) - public String index() { - return "index"; - } - - @RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greet() { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greetWithPathVariable(@PathVariable("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public Greeting greetWithQueryVariable(@RequestParam("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } - - @RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public Greeting greetWithPost() { - Greeting greeting = new Greeting(); - greeting.setId(1); - greeting.setMessage("Hello World!!!"); - return greeting; - } - @RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) { - Greeting greeting = new Greeting(); - greeting.setId(id); - greeting.setMessage("Hello World " + name + "!!!"); - return greeting; - } + @RequestMapping(value = "/homePage", method = RequestMethod.GET) + public String index() { + return "index"; + } + + @RequestMapping(value = "/greet", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greet() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPathVariable/{name}", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithPathVariable(@PathVariable("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithQueryVariable", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public Greeting greetWithQueryVariable(@RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPost", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPost() { + Greeting greeting = new Greeting(); + greeting.setId(1); + greeting.setMessage("Hello World!!!"); + return greeting; + } + + @RequestMapping(value = "/greetWithPostAndFormData", method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public Greeting greetWithPostAndFormData(@RequestParam("id") int id, @RequestParam("name") String name) { + Greeting greeting = new Greeting(); + greeting.setId(id); + greeting.setMessage("Hello World " + name + "!!!"); + return greeting; + } } \ No newline at end of file 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.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.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.xml b/spring-mvc-test/src/main/webapp/WEB-INF/web.xml index 8cf7a9a37b..dc0233a7fc 100644 --- a/spring-mvc-test/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-test/src/main/webapp/WEB-INF/web.xml @@ -1,34 +1,33 @@ - + - Spring MVC - Integration Testing + Spring MVC - Integration Testing - - contextClass - + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - contextConfigLocation - com.baeldung.spring - - - - org.springframework.web.context.ContextLoaderListener - + + + contextConfigLocation + com.baeldung.spring + - - spring - org.springframework.web.servlet.DispatcherServlet - 1 - - - - 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..368ef6ec91 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 verifyWac() { + 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 verifyIndexJspViewName() 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 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 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 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 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 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 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 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 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 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 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 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)); + } } \ 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 155b6b4a50..1631118981 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 @@ -15,69 +15,49 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; public class GreetControllerTest { - - private MockMvc mockMvc; - - @Before - public void setup() { - this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build(); - } - - @Test - public void verifyIndexJspViewName() 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!!!")); - } - - @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!!!")); - } - - @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!!!")); - } - - @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 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!!!")); - } + private MockMvc mockMvc; - @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)); - } + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build(); + } + + @Test + public void verifyIndexJspViewName() 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!!!")); + } + + @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!!!")); + } + + @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!!!")); + } + + @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 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 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)); + } } From 709da6bcd28e55e71e010189847e662f37f963a5 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Tue, 9 Aug 2016 10:14:59 +0530 Subject: [PATCH 4/5] Updated the test names and added Annotation Based Web Config. --- .../com/baeldung/web/WebAppInitializer.java | 33 +++++++++++++++++++ ...ing-servlet.xml => spring-servlet-old.xml} | 0 .../webapp/WEB-INF/{web.xml => web-old.xml} | 0 .../GreetControllerIntegrationTest.java | 16 ++++----- .../controller/GreetControllerTest.java | 14 ++++---- 5 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java rename spring-mvc-test/src/main/webapp/WEB-INF/{spring-servlet.xml => spring-servlet-old.xml} (100%) rename spring-mvc-test/src/main/webapp/WEB-INF/{web.xml => web-old.xml} (100%) 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/spring-servlet.xml b/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml similarity index 100% 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 diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/web.xml b/spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml similarity index 100% rename from spring-mvc-test/src/main/webapp/WEB-INF/web.xml rename to spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml 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 368ef6ec91..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 @@ -39,7 +39,7 @@ public class GreetControllerIntegrationTest { } @Test - public void verifyWac() { + public void givenWAC_whenServletContext_thenItProvidesGreetController() { ServletContext servletContext = wac.getServletContext(); Assert.assertNotNull(servletContext); Assert.assertTrue(servletContext instanceof MockServletContext); @@ -47,42 +47,42 @@ public class GreetControllerIntegrationTest { } @Test - public void verifyIndexJspViewName() throws Exception { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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)); } 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 1631118981..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,39 +24,39 @@ public class GreetControllerTest { } @Test - public void verifyIndexJspViewName() throws Exception { + public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception { this.mockMvc.perform(get("/homePage")).andExpect(view().name("index")); } @Test - public void verifyGreet() throws Exception { + 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 { + 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 { + 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 { + 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 { + 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 { + 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)); } From aed7a46324bf4ae596c7f5f0d00332e497f26afd Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sun, 14 Aug 2016 09:45:23 +0200 Subject: [PATCH 5/5] BAEL-175 - moving to mvc java module --- spring-mvc-java/pom.xml | 5 + .../java/com/baeldung/model}/Greeting.java | 2 +- .../spring/web/config}/ApplicationConfig.java | 4 +- .../web}/controller/GreetController.java | 11 +- .../src/main/webapp/WEB-INF/jsp/index.jsp | 0 .../GreetControllerIntegrationTest.java | 30 +-- .../web}/controller/GreetControllerTest.java | 26 ++- spring-mvc-test/README | 5 - spring-mvc-test/pom.xml | 193 ------------------ .../com/baeldung/web/WebAppInitializer.java | 33 --- .../src/main/resources/logback.xml | 18 -- .../webapp/WEB-INF/spring-servlet-old.xml | 5 - .../src/main/webapp/WEB-INF/web-old.xml | 33 --- 13 files changed, 39 insertions(+), 326 deletions(-) rename {spring-mvc-test/src/main/java/com/baeldung/spring/bean => spring-mvc-java/src/main/java/com/baeldung/model}/Greeting.java (90%) rename {spring-mvc-test/src/main/java/com/baeldung/spring => spring-mvc-java/src/main/java/com/baeldung/spring/web/config}/ApplicationConfig.java (92%) rename {spring-mvc-test/src/main/java/com/baeldung/spring => spring-mvc-java/src/main/java/com/baeldung/web}/controller/GreetController.java (83%) rename {spring-mvc-test => spring-mvc-java}/src/main/webapp/WEB-INF/jsp/index.jsp (100%) rename {spring-mvc-test/src/test/java/com/baeldung/spring => spring-mvc-java/src/test/java/com/baeldung/web}/controller/GreetControllerIntegrationTest.java (85%) rename {spring-mvc-test/src/test/java/com/baeldung/spring => spring-mvc-java/src/test/java/com/baeldung/web}/controller/GreetControllerTest.java (55%) delete mode 100644 spring-mvc-test/README delete mode 100644 spring-mvc-test/pom.xml delete mode 100644 spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java delete mode 100644 spring-mvc-test/src/main/resources/logback.xml delete mode 100644 spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml delete mode 100644 spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 33d6306c5a..4d3432d47b 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -111,6 +111,11 @@ ${mockito.version} test + + com.jayway.jsonpath + json-path + 2.2.0 + org.springframework spring-test diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java b/spring-mvc-java/src/main/java/com/baeldung/model/Greeting.java similarity index 90% rename from spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Greeting.java index 11c0a79b0e..db021b8e8c 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Greeting.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.bean; +package com.baeldung.model; public class Greeting { private int id; diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java similarity index 92% rename from spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java index 09be5ee113..261d5793dc 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java @@ -1,4 +1,4 @@ -package com.baeldung.spring; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -12,7 +12,7 @@ import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration -@ComponentScan(basePackages = { "com.baeldung.spring.controller" }) +@ComponentScan(basePackages = { "com.baeldung.web.controller" }) public class ApplicationConfig extends WebMvcConfigurerAdapter { public ApplicationConfig() { diff --git a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/GreetController.java similarity index 83% rename from spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java rename to spring-mvc-java/src/main/java/com/baeldung/web/controller/GreetController.java index d563f80918..6f764fedfb 100644 --- a/spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/GreetController.java @@ -1,13 +1,8 @@ -package com.baeldung.spring.controller; +package com.baeldung.web.controller; +import com.baeldung.model.Greeting; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; - -import com.baeldung.spring.bean.Greeting; +import org.springframework.web.bind.annotation.*; @Controller public class GreetController { diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/jsp/index.jsp similarity index 100% rename from spring-mvc-test/src/main/webapp/WEB-INF/jsp/index.jsp rename to spring-mvc-java/src/main/webapp/WEB-INF/jsp/index.jsp diff --git a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java similarity index 85% rename from spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java rename to spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java index abed0a977e..61e0f632f1 100644 --- a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java @@ -1,5 +1,7 @@ -package com.baeldung.spring.controller; +package com.baeldung.web.controller; + +import com.baeldung.spring.web.config.ApplicationConfig; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -11,21 +13,18 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.ApplicationConfig; - -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - import javax.servlet.ServletContext; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = { ApplicationConfig.class }) +@ContextConfiguration(classes = {ApplicationConfig.class}) public class GreetControllerIntegrationTest { @Autowired @@ -33,6 +32,9 @@ public class GreetControllerIntegrationTest { private MockMvc mockMvc; + private static final String CONTENT_TYPE = "application/json"; + + @Before public void setup() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); @@ -54,36 +56,36 @@ public class GreetControllerIntegrationTest { @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()); + Assert.assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType()); } @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")) + this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!")); } @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")) + this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World 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!!!")); + .andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")); } @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")) + this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")); } @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)); + .andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).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-java/src/test/java/com/baeldung/web/controller/GreetControllerTest.java similarity index 55% rename from spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java rename to spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerTest.java index 8e624544cd..0fd71a46dd 100644 --- a/spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerTest.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.controller; +package com.baeldung.web.controller; import org.junit.Before; import org.junit.Test; @@ -9,14 +9,12 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; public class GreetControllerTest { private MockMvc mockMvc; + private static final String CONTENT_TYPE = "application/json"; @Before public void setup() { @@ -30,34 +28,34 @@ public class GreetControllerTest { @Test 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!!!")); + this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!")); } @Test 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!!!")); + this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John!!!")); } @Test 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!!!")); + this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World 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!!!")); + this.mockMvc.perform(get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()) + .andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")); } @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!!!")); + this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)) + .andExpect(jsonPath("$.message").value("Hello World!!!")); } @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)); + this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()) + .andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1)); } } diff --git a/spring-mvc-test/README b/spring-mvc-test/README deleted file mode 100644 index 9f4a0a60d4..0000000000 --- a/spring-mvc-test/README +++ /dev/null @@ -1,5 +0,0 @@ -To compile and run the project, execute following command: - - mvn clean install org.codehaus.cargo:cargo-maven2-plugin:run - -URL: http://localhost:8080/spring-mvc-test/ diff --git a/spring-mvc-test/pom.xml b/spring-mvc-test/pom.xml deleted file mode 100644 index 3c4875f087..0000000000 --- a/spring-mvc-test/pom.xml +++ /dev/null @@ -1,193 +0,0 @@ - - 4.0.0 - com.baeldung - spring-mvc-test - 0.1-SNAPSHOT - spring-mvc-test - war - - - - 4.3.1.RELEASE - - 1.7.21 - 1.1.7 - - 3.4 - - 1.3 - 4.12 - 1.10.19 - 4.4.5 - 4.5.2 - - - 3.5.1 - 2.6 - 2.19.1 - 2.19.1 - 3.0.1 - - - 1.5.0 - - - - - - - org.springframework - spring-web - ${org.springframework.version} - - - org.springframework - spring-webmvc - ${org.springframework.version} - - - org.springframework - spring-websocket - ${org.springframework.version} - - - - com.fasterxml.jackson.core - jackson-core - 2.7.3 - - - com.fasterxml.jackson.core - jackson-databind - 2.7.3 - - - - javax.servlet - javax.servlet-api - 3.0.1 - provided - - - javax.servlet - jstl - 1.2 - runtime - - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - org.slf4j - slf4j-log4j12 - ${org.slf4j.version} - - - - junit - junit - ${junit.version} - test - - - org.mockito - mockito-core - ${mockito.version} - test - - - com.jayway.jsonpath - json-path - 2.2.0 - - - org.springframework - spring-test - ${org.springframework.version} - test - - - - - spring-mvc-test - - - src/main/resources - true - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - - maven-resources-plugin - 2.7 - - - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - false - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - - - - - org.apache.maven.plugins - maven-failsafe-plugin - ${maven-failsafe-plugin.version} - - - **/*IntegrationTest.java - - - - - - integration-test - verify - - - - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - - jetty8x - embedded - - - - 8080 - - - - - - - \ No newline at end of file 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 deleted file mode 100644 index 23fad058d0..0000000000 --- a/spring-mvc-test/src/main/java/com/baeldung/web/WebAppInitializer.java +++ /dev/null @@ -1,33 +0,0 @@ -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/resources/logback.xml b/spring-mvc-test/src/main/resources/logback.xml deleted file mode 100644 index 166c369905..0000000000 --- a/spring-mvc-test/src/main/resources/logback.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml b/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml deleted file mode 100644 index 2b8192e742..0000000000 --- a/spring-mvc-test/src/main/webapp/WEB-INF/spring-servlet-old.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - \ 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 deleted file mode 100644 index dc0233a7fc..0000000000 --- a/spring-mvc-test/src/main/webapp/WEB-INF/web-old.xml +++ /dev/null @@ -1,33 +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