0

I have a model object like the one below with custom constraint validator. The custom validator checks if either fileName or hours is populated.

@ValidCriteria
class Criteria{
    @NotEmpty String id;    
    @NotEmpty String name;
    String fileName;
    String hours;   
}

There is a method which takes takes this as input and it validates all the following conditions

  1. criteria is not null (by Default validator)
  2. criteria.id is not empty (by Default validator)
  3. criteria.name is not empty (by Default validator)
  4. criteria fileName or hours is not empty (by custom validator)

    void evaluate(@NotNull @Valid Criteria criteria){}

Now when I'm writing unit test for this model class. I'm invoking

Validation.buildDefaultValidatorFactory().getValidator().validate(criteria)

when I do this, the first 3 validations by default validator is not being invoked. Is there a way to invoke the default validator from my unit test or from my custom validator?

1 Answer 1

1

I found the answer. In fact, the below line is invoking the custom validator defined in my custom constraint @ValidCriteria which in turn delegates to default validator. It was not working earlier due to a bug in my validator.

Validation.buildDefaultValidatorFactory().getValidator().validate(criteria)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.