Merge branch 'pr/562-sunil-integration-testing'
Conflicts: pom.xml
This commit is contained in:
commit
d81365a7ac
|
@ -111,6 +111,11 @@
|
|||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.spring.web.config;
|
||||
|
||||
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.web.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.Greeting;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<h1>Spring MVC - Integration Testing</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.web.controller;
|
||||
|
||||
|
||||
import com.baeldung.spring.web.config.ApplicationConfig;
|
||||
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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = {ApplicationConfig.class})
|
||||
public class GreetControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static final String CONTENT_TYPE = "application/json";
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@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 givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/homePage")).andDo(print()).andExpect(MockMvcResultMatchers.view().name("index"));
|
||||
}
|
||||
|
||||
@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(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(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(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(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(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(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.web.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.*;
|
||||
|
||||
public class GreetControllerTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
private static final String CONTENT_TYPE = "application/json";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Exception {
|
||||
this.mockMvc.perform(get("/homePage")).andExpect(view().name("index"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||
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(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(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||
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(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(status().isOk())
|
||||
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue