How to do it...

  1. First, we need to create an object with some fields to be validated:
public class User {    @NotBlank    private String name;        @Email    private String email;        @NotEmpty    private List<@PositiveOrZero Integer> profileId;        public User(String name, String email, List<Integer> profileId) {        this.name = name;        this.email = email;        this.profileId = profileId;    }}
  1. Then we create a test class to validate those constraints:
public class UserTest {     private static Validator validator;     @BeforeClass    public static void setUpClass() {        validator = Validation.buildDefaultValidatorFactory()        .getValidator();    }     @Test    public void validUser() {        User user = new User(            "elder",             "elder@eldermoraes.com",             asList(1,2));  Set<ConstraintViolation<User>> cv = ...

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.