Spring 中的单元测试 JSR-303 验证

发布于 2024-11-01 18:20:30 字数 3087 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-11-08 18:20:30

我使用上下文配置进行了更简单的设置,如下所示:

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 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>
好菇凉咱不稀罕他 2024-11-08 18:20:30

我认为这表明您遇到了版本问题。您拥有的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文