Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-11603
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfig.class })
|
||||
public class CircularDependencyIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Bean
|
||||
public CircularDependencyA getCircularDependencyA() {
|
||||
return new CircularDependencyA();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CircularDependencyB getCircularDependencyB() {
|
||||
return new CircularDependencyB();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCircularDependency_whenSetterInjection_thenItWorks() {
|
||||
final CircularDependencyA circA = context.getBean(CircularDependencyA.class);
|
||||
|
||||
Assert.assertEquals("Hi!", circA.getCircB().getMessage());
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.circulardependency" })
|
||||
public class TestConfig {
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.web.controller.handlermapping.WelcomeController;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
public class BeanNameUrlHandlerMappingConfig {
|
||||
|
||||
@Bean
|
||||
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
|
||||
return new BeanNameUrlHandlerMapping();
|
||||
}
|
||||
|
||||
@Bean("/beanNameUrl")
|
||||
public WelcomeController welcome() {
|
||||
return new WelcomeController();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
|
||||
import com.baeldung.web.controller.handlermapping.WelcomeController;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class HandlerMappingDefaultConfig {
|
||||
|
||||
@Bean("/welcome")
|
||||
public BeanNameHandlerMappingController beanNameHandlerMapping() {
|
||||
return new BeanNameHandlerMappingController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WelcomeController welcome() {
|
||||
return new WelcomeController();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
|
||||
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
|
||||
import com.baeldung.web.controller.handlermapping.WelcomeController;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class HandlerMappingPrioritiesConfig {
|
||||
|
||||
@Bean
|
||||
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
|
||||
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
|
||||
beanNameUrlHandlerMapping.setOrder(1);
|
||||
return beanNameUrlHandlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
|
||||
simpleUrlHandlerMapping.setOrder(0);
|
||||
Map<String, Object> urlMap = new HashMap<>();
|
||||
urlMap.put("/welcome", simpleUrlMapping());
|
||||
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
||||
return simpleUrlHandlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleUrlMappingController simpleUrlMapping() {
|
||||
return new SimpleUrlMappingController();
|
||||
}
|
||||
|
||||
@Bean("/welcome")
|
||||
public BeanNameHandlerMappingController beanNameHandlerMapping() {
|
||||
return new BeanNameHandlerMappingController();
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.web.controller.handlermapping.WelcomeController;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
public class SimpleUrlHandlerMappingConfig {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
|
||||
Map<String, Object> urlMap = new HashMap<>();
|
||||
urlMap.put("/simpleUrlWelcome", welcome());
|
||||
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
||||
return simpleUrlHandlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WelcomeController welcome() {
|
||||
return new WelcomeController();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.handlermappings;
|
||||
|
||||
import com.baeldung.config.BeanNameUrlHandlerMappingConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.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.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
|
||||
public class BeanNameMappingConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBeanNameMapping_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/beanNameUrl")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.handlermappings;
|
||||
|
||||
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.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.config.HandlerMappingDefaultConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
|
||||
public class HandlerMappingDefaultConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDefaultConfig_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.handlermappings;
|
||||
|
||||
import com.baeldung.config.HandlerMappingPrioritiesConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.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.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
|
||||
public class HandlerMappingPriorityConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfiguringPriorities_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("simple-url-handler-mapping")).andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.handlermappings;
|
||||
|
||||
import com.baeldung.config.SimpleUrlHandlerMappingConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.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.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
|
||||
public class SimpleUrlMappingConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSimpleUrlMapping_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/simpleUrlWelcome")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
public class ClassValidationMvcIntegrationTest {
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new NewUserController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMatchingEmailPassword_whenPostNewUserForm_thenOk() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/user")
|
||||
.accept(MediaType.TEXT_HTML)
|
||||
.param("email", "john@yahoo.com")
|
||||
.param("verifyEmail", "john@yahoo.com")
|
||||
.param("password", "pass")
|
||||
.param("verifyPassword", "pass"))
|
||||
.andExpect(model().attribute("message", "Valid form"))
|
||||
.andExpect(view().name("userHome"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNotMatchingEmailPassword_whenPostNewUserForm_thenOk() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/user")
|
||||
.accept(MediaType.TEXT_HTML)
|
||||
.param("email", "john@yahoo.com")
|
||||
.param("verifyEmail", "john@yahoo.commmm")
|
||||
.param("password", "pass")
|
||||
.param("verifyPassword", "passsss"))
|
||||
.andExpect(model().errorCount(2))
|
||||
.andExpect(view().name("userHome"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
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.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
public class CustomMVCValidatorIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new ValidatedPhoneController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPhonePageUri_whenMockMvc_thenReturnsPhonePage() throws Exception{
|
||||
this.mockMvc.perform(get("/validatePhone")).andExpect(view().name("phoneHome"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPhoneURIWithPostAndFormData_whenMockMVC_thenVerifyErrorResponse() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/addValidatePhone").
|
||||
accept(MediaType.TEXT_HTML).
|
||||
param("phoneInput", "123")).
|
||||
andExpect(model().attributeHasFieldErrorCode("validatedPhone", "phone","ContactNumberConstraint")).
|
||||
andExpect(view().name("phoneHome")).
|
||||
andExpect(status().isOk()).
|
||||
andDo(print());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Assert;
|
||||
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 com.baeldung.model.Employee;
|
||||
import com.baeldung.spring.web.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class EmployeeIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private EmployeeController employeeController;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
employeeController.initEmployees();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitEmployees_thenVerifyValuesInitiation() {
|
||||
|
||||
Employee employee1 = employeeController.employeeMap.get(1L);
|
||||
Employee employee2 = employeeController.employeeMap.get(2L);
|
||||
Employee employee3 = employeeController.employeeMap.get(3L);
|
||||
|
||||
Assert.assertTrue(employee1.getId() == 1L);
|
||||
Assert.assertTrue(employee1.getName().equals("John"));
|
||||
Assert.assertTrue(employee1.getContactNumber().equals("223334411"));
|
||||
Assert.assertTrue(employee1.getWorkingArea().equals("rh"));
|
||||
|
||||
Assert.assertTrue(employee2.getId() == 2L);
|
||||
Assert.assertTrue(employee2.getName().equals("Peter"));
|
||||
Assert.assertTrue(employee2.getContactNumber().equals("22001543"));
|
||||
Assert.assertTrue(employee2.getWorkingArea().equals("informatics"));
|
||||
|
||||
Assert.assertTrue(employee3.getId() == 3L);
|
||||
Assert.assertTrue(employee3.getName().equals("Mike"));
|
||||
Assert.assertTrue(employee3.getContactNumber().equals("223334411"));
|
||||
Assert.assertTrue(employee3.getWorkingArea().equals("admin"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
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.ResultMatcher;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.spring.web.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RequestMapingShortcutsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext ctx;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup () {
|
||||
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx);
|
||||
this.mockMvc = builder.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get");
|
||||
|
||||
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response");
|
||||
|
||||
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
|
||||
|
||||
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response");
|
||||
|
||||
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put");
|
||||
|
||||
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response");
|
||||
|
||||
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
|
||||
|
||||
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response");
|
||||
|
||||
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch");
|
||||
|
||||
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response");
|
||||
|
||||
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
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.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.web.controller.SimpleBookController;
|
||||
|
||||
public class SimpleBookControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||
this.mockMvc
|
||||
.perform(get("/books/42"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(CONTENT_TYPE))
|
||||
.andExpect(jsonPath("$.id").value(42));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
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.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.web.controller.SimpleBookController;
|
||||
|
||||
public class SimpleBookRestControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookController()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||
this.mockMvc
|
||||
.perform(get("/books/42"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(CONTENT_TYPE))
|
||||
.andExpect(jsonPath("$.id").value(42));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:cache="http://www.springframework.org/schema/cache"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/cache
|
||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
|
||||
</bean>
|
||||
|
||||
<bean name="beanNameUrl" class="com.baeldung.WelcomeController" />
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -1,35 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:cache="http://www.springframework.org/schema/cache"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/cache
|
||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
||||
</bean>
|
||||
|
||||
<!-- register beans -->
|
||||
<bean class="com.baeldung.WelcomeController" />
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -1,45 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:cache="http://www.springframework.org/schema/cache"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/cache
|
||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
|
||||
<property name="order" value="2" />
|
||||
</bean>
|
||||
<bean name="/welcome" class="com.baeldung.BaeldungController" />
|
||||
|
||||
|
||||
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
|
||||
<property name="mappings">
|
||||
<value>
|
||||
/welcome=test
|
||||
/*/welcome=test
|
||||
</value>
|
||||
</property>
|
||||
<property name="order" value="0" />
|
||||
</bean>
|
||||
<bean id="test" class="com.baeldung.TestController" />
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
||||
<property name="order" value="1" />
|
||||
</bean>
|
||||
<bean class="com.baeldung.WelcomeController" />
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -1,38 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:cache="http://www.springframework.org/schema/cache"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||
http://www.springframework.org/schema/cache
|
||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||
|
||||
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
|
||||
|
||||
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
|
||||
<property name="mappings">
|
||||
<value>
|
||||
/simpleUrlWelcome=welcome
|
||||
/*/simpleUrlWelcome=welcome
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="welcome" class="com.baeldung.WelcomeController" />
|
||||
|
||||
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user