- Mastering Spring 5.0
- Ranga Rao Karanam
- 94字
- 2021-07-02 22:12:14
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.