The complete Test method is listed as follows:
@Test
public void basicTest_WithAllValidationErrors() throws Exception {
this.mockMvc
.perform(
post("/create-user-with-validation")
.accept(MediaType.parseMediaType("application/html;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(model().errorCount(4))
.andExpect(model().attributeHasFieldErrorCode("user", "name", "Size"));
}
The following definitions explain the workings of the preceding code block:
- post("/create-user-with-validation"): This creates an HTTP POST request to the specified URI. Since we are not passing any request parameters, all attributes are null. This will trigger validation errors.
- model().errorCount(4): This checks whether there are four validation errors on the model.
- model().attributeHasFieldErrorCode("user", "name", "Size"): This checks whether the user attribute has a name field with the validation error named Size.