The Test method

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"));
}

Some points to note here are as follows:

  • post("/create-user-with-validation"): 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): Checks whether there are four validation errors on the model.
  • model().attributeHasFieldErrorCode("user", "name", "Size"): Checks whether the user attribute has a field name with the validation error named Size.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset