vendredi 1 novembre 2019

Test ConstraintValidator with @Service

I have this ConstraintValidator which I am trying to test...

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

    public UniqueFurlValidator() {}

    @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;
    }
}

The test I have written so far is...

@RunWith(SpringRunner.class)
@DataJpaTest
public class UniqueFurlValidatorTest {
    @Mock
    private EntityManager entityManager;

    @Autowired
    private SponsorService sponsorService;

    @InjectMocks
    private UniqueFurlValidator validator;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void alreadyExistsFurlReturnsError() {

    }
}

The @Service links to the JPA repository. Please advice on how I can actually test this.

Aucun commentaire:

Enregistrer un commentaire