As used herein springboot version: <version> 2.1.1.RELEASE </ version>
Comes hibernate.validator, so do not rely on additional
1, corresponding to the front desk to create a form of test vo, and add a comment field validation
import java.math.BigDecimal; import java.util.Date; import java.util.List; import javax.validation.constraints.AssertTrue; import javax.validation.constraints.DecimalMax; import javax.validation.constraints.DecimalMin; import javax.validation.constraints.Digits; import javax.validation.constraints.Future; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Null; import javax.validation.constraints.Past; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.Range; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; public class TestVO { // 判断字符串空用这个 @NotBlank(message="姓名必须输入!") private String name; @NotBlank @Length (min = 18 is, max =. 19, Message = "ID must be between 18-19" ) Private String Card; @NotNull @Past (the Message = "date must be before the day" ) // @Future // foreground delivery date character, automatically converted into date objects @DateTimeFormat (pattern = "yyyy-MM-dd HH: mm: SS" ) // date object is output to the front, showing the automatic formatting @JsonFormat (pattern = "the mM-dd-YYYY HH: mm: SS", TimeZone = "GMT +. 8" ) Private a date dATE; // the basic types of this space with @NotNull (message = "Age must be entered!" ) @Max (the Message = "maximum age of 28 years old!", Value = 28 ) @Min (the Message "! Minimum age of 18 years old" =, value = 18 ) Private Integer Age; @NotBlank // String, numeric size determination @Range (min = 1, max = 100, message = "weight can only be between 1 - 100" ) // array, the set size determination // @Size (max = 100, min = 1, message = "size only between 1-100") @digits (= integer. 3, 2 = fraction, Message = "weight can only be a number, an integer of 3 or less, to retain two decimal" ) Private String weight ; @NotNull @AssertTrue (the Message = "Sex can only fill male!" ) // @AssertFalse Private Boolean Sex; // judging by this collection of empty @NotEmpty (message = "collection can not be empty!" ) List<String> list; @Null (Message = "This field value is not set!" ) // @NotNull Private Object tmp; @NotBlank @Pattern (regexp = "^ [150 [0-9] +]. 11 {}", Message = "! Phone format issue" ) Private String Phone; @NotBlank @Email (the Message = "! Email format is incorrect" ) Private String Email; @DecimalMin (value = "18", Message = "dicimal not less than 18!" ) @DecimalMax(value="20",message="dicimal不能大于20!") private BigDecimal dicimal; // Getter..Setter .. omitted }
2, to create the test control layer controller receives parameters
@RestController public class TestController { @RequestMapping(value="/v") public void validateTestVO(@Valid TestVO testVO,BindingResult result) { if(result.hasErrors()){ List<ObjectError> list = result.getAllErrors(); for(ObjectError error:list){ System.out.println(error.getCode()+"-"+error.getDefaultMessage()); } } } }
3. Create a controller class test, test verification
public class ControllerTest { Private MockMvc mockMvc; // @Before annotated representation takes precedence when the start of the test, generally used as a resource initialization. // this class initialization controller generates a single embodiment @Before public void the setUp () throws Exception { mockMvc=MockMvcBuilders.standaloneSetup(new TestController()).build(); } @Test public void controllerTest()throws Exception{ mockMvc.perform(MockMvcRequestBuilders.post("/v") .param("age", "28") // .param("name", "aaa") // .param("list", "[\"bb\",\"cc\"]") // .param("card", "123456789012345678") // .param("date", "2019-10-01 11:09:11") // .param("weight", "99.99") // .param("sex", "true") //// .param("tmp", "") //// .param("phone", "") // .param("dicimal", "18") // .param("email", "aaa") ); } }