Unit testing JSR-303 validation in Spring
I am trying to add a unit test for an annotated bean in spring using JSR-303 validation.
The bean is a simple one like this:
public class Bean {
@Size(max=XX)
String text;
}
In the Spring config I have the all the necessary JAR-files:
validation-api
hibernate-validator
The validator is initialized with:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
My attempt at a testcase looks like this (the spring and other fluff removed):
@Autowired
private LocalValidatorFactoryBean factory;
@Test
public void testTemplateFormBeanValidation() {
// TODO exception at the moment. Can not resolve the validator. Provider issue.
factory.setProviderClass(HibernateValidator.class);
factory.setTraversableResolver(new DefaultTraversableResolver());
Validator val = factory.getValidator();
TemplateFormBean fb1 = new TemplateFormBean();
fb1.setText("this text works");
Set<ConstraintViolation<TemplateFormBean>> constraints = val.validate(fb1);
assertNull(constraints);
}
When I run this test I get the following stacktrace:
java.lang.AbstractMethodError: javax/persistence/spi/PersistenceProvider.getProviderUtil()Ljavax/persistence/spi/ProviderUtil;
at javax.persistence.Persistence$1.isLoaded(Persistence.java:78)
at org.hibernate.validator.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:62)
at org.hibernate.validator.engine.resolver.DefaultTraversableResolver.isReachable(DefaultTraversableResolver.java:94)
at org.hibernate.validator.engine.resolver.SingleThreadCachedTraversableResolver.isReachable(SingleThreadCachedTraversableResolver.java:47)
at org.hibernate.validator.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:757)
at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:324)
at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:273)
at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:256)
at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:210)
at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:145)
at general.TestValidationBeans.testTemplateFormBeanValidation(TestValidationBeans.java:30)
Am I missing some dependencies? I am using Hibernate with JPA2 annotations as persistence, so all those JARs exist and are working.
The version of Hibernate is:
2011-04-19 08:37:47 [INFO] Version - Hibernate Commons Annotations 3.2.0.Final
2011-04-19 08:37:47 [INFO] Environment - Hibernate 3.6.2.Final
2011-04-19 08:37:47 [INFO] Environment - hibernate.properties not found
2011-04-19 08:37:47 [INFO] Environment - Bytecode provider name : javassist
...
2011-04-19 08:37:47 [INFO] Version - Hibernate Validator 4.1.0.Final
Why isn´t Hibernate logging the version of hibernate-annotations? According to docs it is bundled from hibernate 3.5 开发者_运维问答in hibernate3.jar which I am using.
Should I see log output for hibernate annotations (something like "Version - Hibernate Annotations 3.4.0 GA)?I have a simpler set-up using context configuration as follows:
MyTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
@Autowired
private Validator validator;
@Test
public void testMyForm() {
final MyForm form = new MyForm();
.
.
final Set<ConstraintViolation<MyForm>> violations = validator.validate(form);
assertTrue(violations.isEmpty());
}
}
MyTest-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>
I think this says you've got a version problem. The version of the JPA JAR that you have has a different method signature from the one that Spring expects. Try updating the JARs to use the ones from Spring and see if that helps.
精彩评论