Issue
I've been reading the Hibernate docs about class validation tests and I did not found any solution to my "problem". I would like to test my custom validators. For example, let's say I have the following class:
public class User {
@NotEmpty(message = "{username.notSpecified}")
private String username;
@NotEmpty
@Size(min = 6, message = "{password.tooShort")
private String password;
@NotEmpty(message = "{city.notSpecified}")
private String city;
/* getters & setters omitted */
}
And I want to check that every user is located in Barcelona city. For that, I would have my custom user validator implementation as follows:
public class UserValidator implements Validator {
@Autowired
private Validator validator;
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
validator.validate(target,errors);
User user = (User) target;
if(!(user.getCity().equals("Barcelona"))){
errors.rejectValue("city", "city.notValid", "Invalid city");
}
}
}
I have no idea how to use this custom validator instead of the default one provided in the example and that only checks annotated field constraints and not more "business logic" ones. Any examples or clues on that?
Thanks!
Solution
I have finally found a solution:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/test-applicationContext.xml"})
public class UserValidatorTest {
private static Logger logger = Logger.getLogger(UserValidatorTest.class);
@Autowired
private UserValidator validator; //my custom validator, bean defined in the app context
@Test
public void testUserValidator(){
User user = new User("name", "1234567", "Barcelona");
BindException errors = new BindException(user, "user");
ValidationUtils.invokeValidator(validator, user, errors);
Assert.assertFalse(errors.hasErrors());
}
}
I have checked validation groups (any annotation with "groups" specified) and they are also working with the invokeValidator static method (by simply adding them after the errors parameter).
Answered By - jarandaf