我想在JSR-303驗證的春天爲註釋bean添加一個單元測試。春季單元測試JSR-303驗證
的bean是一個簡單的像這樣:
public class Bean {
@Size(max=XX)
String text;
}
在Spring配置我有所有必要的JAR文件:
:validation-api
hibernate-validator
驗證程序是使用初始化
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
我的測試用例看起來像這樣(去除了彈簧和其他絨毛):
@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);
}
當我運行這個測試,我得到以下堆棧跟蹤:
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)
我缺少一些依賴?我使用Hibernate和JPA2註釋作爲持久性,因此所有這些JAR都存在並正在工作。
的Hibernate的版本是:
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
爲什麼isn't Hibernate日誌的Hibernate註解的版本?根據文檔,它是從我正在使用的hibernate3.jar中的hibernate 3.5中捆綁的。
我應該看到日誌Hibernate註解輸出(類似「版本 - Hibernate註解3.4.0 GA)?
我可以補充一點,它的工作原理使用此代碼在控制器:@Valid TemplateFormBean FB,BindingResult結果。所以我相信我錯過了一些配置時使用這種編程方式。 – Daniel 2011-04-19 12:53:29
運行WebSphere Application Server 7時出現同樣的錯誤,該錯誤包含JPA 1.0並且正在搞亂休眠JPA 2.0的實現。所以我的猜測是,在你的類路徑的某個地方,你有JPA 1.0類 – Tommy 2012-07-04 15:43:28