[BAEL-19881] - Rename spring-mvc-simple modules
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import com.baeldung.spring.configuration.ApplicationConfiguration;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes={ApplicationConfiguration.class})
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.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 org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsNotPassed_thenResponse500() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isInternalServerError());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.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 org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithOptionalParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.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 org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithRequiredAttributeIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.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 org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithMapParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.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 org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithTwoSeparateMethodsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.controller.push;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.spring.configuration.PushConfiguration;
|
||||
|
||||
@SpringJUnitWebConfig(PushConfiguration.class)
|
||||
public class PushControllerIntegrationTest {
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithoutPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithoutPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.controller.rss;
|
||||
|
||||
import com.baeldung.spring.configuration.ApplicationConfiguration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringJUnitWebConfig(ApplicationConfiguration.class)
|
||||
public class ArticleRssIntegrationTest {
|
||||
public static final String APPLICATION_RSS_XML = "application/rss+xml";
|
||||
public static final String APPLICATION_RSS_JSON = "application/rss+json";
|
||||
public static final String APPLICATION_RSS_XML_CHARSET_UTF_8 = "application/rss+xml;charset=UTF-8";
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingXMLFeed_thenContentTypeIsOk() throws Exception {
|
||||
mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_XML))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(APPLICATION_RSS_XML_CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingJSONFeed_thenContentTypeIsOk() throws Exception {
|
||||
mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(APPLICATION_RSS_JSON));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.spring.servlets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class HelloServletIntegrationTest {
|
||||
@Test
|
||||
public void whenRequested_thenForwardToCorrectUrl() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello");
|
||||
request.addParameter("name", "Dennis");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
HelloServlet servlet = new HelloServlet();
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
assertEquals("/forwarded", response.getForwardedUrl());
|
||||
assertEquals(200, response.getStatus());
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.spring.servlets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class WelcomeServletIntegrationTest {
|
||||
@Test
|
||||
public void whenRequested_thenRedirectedToCorrectUrl() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome");
|
||||
request.addParameter("name", "Dennis");
|
||||
WelcomeServlet servlet = new WelcomeServlet();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
assertEquals("/redirected", response.getRedirectedUrl());
|
||||
assertEquals(302, response.getStatus());
|
||||
}
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
package org.baeldung.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
import org.baeldung.controller.student.Student;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class }, loader = AnnotationConfigWebContextLoader.class)
|
||||
public class ControllerAnnotationIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private Student selectedStudent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
|
||||
selectedStudent = new Student();
|
||||
selectedStudent.setId(1);
|
||||
selectedStudent.setName("Peter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTestController() throws Exception {
|
||||
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
||||
|
||||
// validate modal data
|
||||
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||
|
||||
// validate view name
|
||||
Assert.assertSame(mv.getViewName(), "welcome");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestController() throws Exception {
|
||||
|
||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||
|
||||
ObjectMapper reader = new ObjectMapper();
|
||||
|
||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||
|
||||
Assert.assertEquals(selectedStudent, studentDetails);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestAnnotatedController() throws Exception {
|
||||
|
||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||
|
||||
ObjectMapper reader = new ObjectMapper();
|
||||
|
||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||
|
||||
Assert.assertEquals(selectedStudent, studentDetails);
|
||||
}
|
||||
|
||||
}
|
||||
-77
@@ -1,77 +0,0 @@
|
||||
package org.baeldung.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.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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import org.baeldung.controller.student.Student;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration({ "classpath:test-mvc.xml" })
|
||||
public class ControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private Student selectedStudent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
|
||||
selectedStudent = new Student();
|
||||
selectedStudent.setId(1);
|
||||
selectedStudent.setName("Peter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTestController() throws Exception {
|
||||
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
||||
|
||||
// validate modal data
|
||||
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||
|
||||
// validate view name
|
||||
Assert.assertSame(mv.getViewName(), "welcome");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestController() throws Exception {
|
||||
|
||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||
|
||||
ObjectMapper reader = new ObjectMapper();
|
||||
|
||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||
|
||||
Assert.assertEquals(selectedStudent, studentDetails);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestAnnotatedController() throws Exception {
|
||||
|
||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||
|
||||
ObjectMapper reader = new ObjectMapper();
|
||||
|
||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||
|
||||
Assert.assertEquals(selectedStudent, studentDetails);
|
||||
}
|
||||
}
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
package org.baeldung.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.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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* This is the test class for {@link org.baeldung.controller.controller.PassParametersController} class.
|
||||
* 09/09/2017
|
||||
*
|
||||
* @author Ahmet Cetin
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration({"classpath:test-mvc.xml"})
|
||||
public class PassParametersControllerIntegrationTest {
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModel() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/showViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelMap() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/printViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelAndView() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/goToViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user