Question about the following code:
assertEquals (1, constraintViolations.size ());
assertEquals ("size must be between 2 and 14", constraintViolations.iterator (). next (). getMessage ());
1. It is not clear why it is compared in the string assertEquals (1, constraintViolations.size ()); with a unit?
After all, a unit is compared with a length of Set, if there is a ConsrtainViolations in the set, then there are violations, what is the logic in such a comparison?
2. The second method compares whether "size must be between 2 and 14"
equals to constraintViolations.iterator().next().getMessage())
, that is, whether
"size must be between 2 and 14"
with "size must be between 2 and 14"
?
@BeforeClass public static void setUp(){ ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void licensePlateTooShort(){ Car car = new Car("Moris","D", 4); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); assertEquals(1,constraintViolations.size()); assertEquals("size must be between 2 and 14", constraintViolations.iterator().next().getMessage()); } public class Car { public Car(String manufacturer, String licensePlate, int seatCount) { super(); this.manufacturer = manufacturer; this.licensePlate = licensePlate; this.seatCount = seatCount; } public String getManufacturer() { return manufacturer; } public String getLicensePlate() { return licensePlate; } public int getSeatCount() { return seatCount; } @NotNull private String manufacturer; @NotNull @Size(min = 2, max= 14) private String licensePlate; @NotNull @Min(2) private int seatCount;