I use javax.validation with Spring. In my test (groovy) I explicitly create validator.
import javax.validation.Validation
import javax.validation.Validator
import javax.validation.ValidatorFactory
ValidatorFactory factory = Validation.buildDefaultValidatorFactory()
Validator validator = factory.getValidator()
when:
Set<ConstraintViolation<User>> constraints = validator.validate(entity)
My validator in java
public class EntityDynamicValidator implements ConstraintValidator<SomeConstraint, Entity> {
private GroupService groupService;
// This constructor is required, see the link bellow.
public UserDynamicEnumValidator() {
}
public UserDynamicEnumValidator(final GroupService groupService) {
this.groupService = groupService;
}
@Override
public boolean isValid(final Entity entity, final ConstraintValidatorContext context) {
Something something = groupService.findByValue(entity.getValue());
// Validate all this stuff
}
}
I need to pass a Spring service to the validator.
I have a default constructor because there is an issue Spring unit test issue with Validator
I also tried to solve this, however the tests are fully low level unit tests on my side and I wanted to avoid too much of a context. I couldn't even use your approach. The solution my side was to add a custom ConstraintValidatorBean into the mockmvc. That custom implementation could get a list of validator class Class objects and so if the factory tried to create a validator, I returned a Mockito.mock version of it instead of the real one and I could also look that up for expectation settings.
Feel free to share your knowledge. This is how I solved this task.
I created a Spring component and it has a static field (GroupService
which is initialized in constructor).
@Component // Spring component.
class ServiceHolderComponent {
private static GroupService GROUP_SERVICE;
@Autowired4
public ServiceHolderComponent(final GroupService groupService) {
GROUP_SERVICE = Validate.notNull(groupService); //apache lib
}
public static GroupService getGroupService() {
return GROUP_SERVICE;
}
}
And now validator with default constructor.
public class EntityDynamicValidator implements ConstraintValidator<SomeConstraint, Entity> {
private GroupService groupService;
public UserDynamicEnumValidator() {
this(ServiceHolderComponent.getGroupService());
}
public UserDynamicEnumValidator(final GroupService groupService) {
this.groupService = groupService;
}
@Override
public boolean isValid(final Entity entity, final ConstraintValidatorContext context) {
Something something = groupService.findByValue(entity.getValue());
// Validate all this stuff
}
}
来源:https://stackoverflow.com/questions/55635585/javax-validator-with-spring-component