Spring 中的单元测试 JSR-303 验证
我正在尝试使用 JSR-303 验证在 Spring 中为带注释的 bean 添加单元测试。
bean 是一个像这样的简单 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)
我是否缺少一些依赖项?我使用带有 JPA2 注释的 Hibernate 作为持久性,因此所有这些 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
为什么 Hibernate 不记录 hibernate-annotations 的版本?根据文档,它是从我正在使用的 hibernate3.jar 中的 hibernate 3.5 捆绑的。
我应该看到 hibernate 注释的日志输出(类似于“版本 - Hibernate Annotations 3.4.0 GA”)吗?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用上下文配置进行了更简单的设置,如下所示:
MyTest.java
MyTest-context.xml
I have a simpler set-up using context configuration as follows:
MyTest.java
MyTest-context.xml
我认为这表明您遇到了版本问题。您拥有的 JPA JAR 版本具有与 Spring 期望的方法签名不同的方法签名。尝试更新 JAR 以使用 Spring 中的 JAR,看看是否有帮助。
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.