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).