How it works...

Our User class uses three of the new constraints introduced by Bean Validation 2.0:

  • @NotBlank: Assures that the value is not null, empty, or an empty string (it trims the value before evaluation, to make sure there aren't spaces).
  • @Email: Allows only a valid email format. Forget those crazy JavaScript functions!
  • @NotEmpty: Ensures that a list has at least one item.
  • @PositiveOrZero: Guarantees that a number is equal or greater than zero.

Then we create a test class (using JUnit) to test our validations. It first instantiates Validator:

@BeforeClasspublic static void setUpClass() {    validator = Validation.buildDefaultValidatorFactory().getValidator();}

Validator is an API that validates beans according to the constraints defined ...

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.