jeudi 31 octobre 2019

Test CustomValidator which depends on JPA repository

I have this validator...

public class UniqueFurlValidator implements ConstraintValidator<Unique, String> {
    @Autowired
    private SponsorService sponsorService;

    @Override
    public void initialize(Unique unique) {
        unique.message();
    }

    @Override
    public boolean isValid(String furl, ConstraintValidatorContext context) {
        if (sponsorService != null && sponsorService.existsByFurl(furl)) {
            return false;
        }
        return true;
    }
}

It is used in a form as @Unique and the controller uses it as @Valid. How can I test this? Because the code depends on the repository and if the furl already exists then it shows an error message on the web page saying that it is not unique.

Aucun commentaire:

Enregistrer un commentaire