Java Reference
In-Depth Information
=Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Student student = new Student();
student.setId("ST23576");
student.setFirstname("Stelba");
student.setLastname("estelle");
student.setAddress(new Address());
//Only the default group will be test.
Set<ConstraintViolation<Student>>
constraintViolations
=validator.validate(student);
assertEquals(0,
constraintViolations.size());
}
So,tovalidatetheattributesofthe
Address
objectatthesametimeasthevalidation
of the
Student
object, you have two options. The first is to list all the groups in the
validate()
method as is the case in the following code:
Student student = new Student();
student.setId("ST23576");
student.setFirstname("Stelba");
student.setLastname("estelle");
student.setAddress(new Address());
Set<ConstraintViolation<Student>>
constraintViolations
=validator.validate(student, Default.class,
AddressCheck.class);
assertEquals(2, constraintViolations.size());
The second method is to use the concept of group conversion via the
@Conver-
tGroup
or
@ConvertGroup.List
for several conversions. As its name implies,
this feature gives you the ability to perform conversions from one group to another to
validate attributes whose constraints belong to a group different from the requested