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; 

    2 answers 2

     assertEquals(1,constraintViolations.size()); 

    checks that the first argument is equal to the second argument, i.e. 1 == constraintViolations.size()

     assertEquals("size must be between 2 and 14", constraintViolations.iterator().next().getMessage()); 

    checks that "size must be between 2 and 14" == constraintViolations.iterator().next().getMessage() , that is, they are equal

    assertEquals() uses the equals() method, if defined. Thus, you can use your principle of comparing objects. If it is not defined, it works like assertSame()

    assertSame() simply compares objects with the operator == , that is, it checks whether the parameters are references to the same object.

    Nice article here

    • 1. That is, if in the first 1 == 1 that is Violation? 2. Not clear about the message ... Where does the message "size must be between 2 and 14" in the ConstraintViolation set come from? - Maks.Burkov
    • @ Maks.Burkov if the condition does not work, then an exception is thrown, that is, if 1 constraintViolations.size() != 1 then throws an exception, the same in the second case. verifies that the text `" size must be between 2 and 14 "` will be equal to the value of the first element in constraintViolations - Senior Pomidor
    • From my example in the code. When comparing a string in the second case, "size must be between 2 and 14" and getMessage () compares "size must be between 2 and 14" == "size must be between 2 and 14" ?? - Maks.Burkov 2:49 pm
    • @ Maks.Burkov assertEquals ("size must be between 2 and 14", constraintViolations.iterator (). Next (). GetMessage ()); checks that "size must be between 2 and 14" == constraintViolations.iterator().next().getMessage() , that is, they are __ that is, we check that they are equal. I don't know what lies in constraintViolations[0].getMessage() , I don't know why it was this line that was chosen. I'm actually saying what's going on. in the code it is checked that they are equal, otherwise an exception is thrown - Senior Pomidor
    • “I don’t know why this line was chosen”: Here I don’t understand what will be taken with the getMessage () method, according to the logic of things the same line should be given .. Where does the value of this line come from? After all, "size must be between 2 and 14" is compared with getMessage () value .. If you look at my CAR code, it does not contain @Size (min = 2, max = 14, message = "must be between 2 and 14" ") - Maks.Burkov 5:14

    Sample code that helped me figure out the validation logic!

    Maybe someone will be useful, simple and clear!

     import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class UserValidation { @NotNull(message = "Can't be null") String firstName; @NotNull(message = "Can't be null") @Size(min = 3 , message = "Need to be more then 3 characters") String lastName; public static void validate (Object object, Validator validator){ Set<ConstraintViolation<Object>> violations = validator.validate(object); System.out.println(violations.size()); for(ConstraintViolation<Object> violation: violations){ System.out.println(violation.getMessage()+" "+violation.getPropertyPath()+" "+violation.getInvalidValue()); } } public static void main(String[] args) { ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); Validator validator = vf.getValidator(); UserValidation user = new UserValidation(); user.firstName = null; user.lastName = "MA"; validate(user, validator); } } 

    ================================================= ===

     2 Need to be more then 3 characters lastName MA Can't be null firstName null