Moved MVC Custom Valitation article code from spring-mvc-java to spring-mvc-basics module
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user