Home > Net >  Can this Set be empty when I validate it with the Validator class?
Can this Set be empty when I validate it with the Validator class?

Time:12-27

I don't really understand how the function isEmpty() can return true when it's validated with a valid DTO object. In the anomalies variable, after I checked with the debugger, we have the following values

interpolatedMessage: "Veuillez renseigner si votre véhicule est dérogatoire ou non." (id=344)
messageTemplate: {vehicule.derogatoire.obligatoire}" (id=348)

org.opentest4j.AssertionFailedError: expected: true but was: false

In this case, the test might be written in a wrong way?

 @Test
    public void testValidator() {

        VehiculeFrontDto vehiculeFrontDto = new VehiculeFrontDto();
        vehiculeFrontDto.setImmat("test");
        vehiculeFrontDto.setMarque("test2");
        vehiculeFrontDto.setModele("test3");
        vehiculeFrontDto.setDatePremiereImmat(new Date());
        vehiculeFrontDto.setSituation(SituationVehiculeEnum.CAPA.getCode());

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        
        Set<ConstraintViolation<VehiculeFrontDto>> anomalies = validator.validate(vehiculeFrontDto, Default.class);
        Assertions.assertTrue(anomalies.isEmpty());

CodePudding user response:

When you validate the DTO with the validator, all validation annotations are checked. Did you maybe forget to set the derogatoire property? Because your message template says {vehicule.derogatoire.obligatoire}. If your DTO was valid, the returned list should indeed be empty, but it is not! You probably need to append your code like this:

        VehiculeFrontDto vehiculeFrontDto = new VehiculeFrontDto();
        vehiculeFrontDto.setImmat("test");
        vehiculeFrontDto.setMarque("test2");
        vehiculeFrontDto.setModele("test3");
        vehiculeFrontDto.setDatePremiereImmat(new Date());
        vehiculeFrontDto.setSituation(SituationVehiculeEnum.CAPA.getCode());
        // added line
        vehiculeFrontDto.setDerogatoire(true); // or false?
  • Related