Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 单元测试JSR-303春季验证_Spring_Hibernate Validator - Fatal编程技术网

Spring 单元测试JSR-303春季验证

Spring 单元测试JSR-303春季验证,spring,hibernate-validator,Spring,Hibernate Validator,我试图在spring中使用JSR-303验证为带注释的bean添加一个单元测试 bean是这样一个简单的bean: public class Bean { @Size(max=XX) String text; } 在Spring配置中,我有所有必要的JAR文件: validation-api hibernate-validator 验证程序初始化为: <bean id="validator" class="org.springframework.vali

我试图在spring中使用JSR-303验证为带注释的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" />
我是否缺少一些依赖项?我使用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
为什么Hibernate日志不是Hibernate注释的版本?根据文档,它是从我正在使用的hibernate3.jar中的hibernate3.5绑定而来的。

我应该看到hibernate注释的日志输出吗(类似于“版本-hibernate注释3.4.0 GA”)?

我想这说明您有一个版本问题。您拥有的JPA JAR版本具有与Spring期望的不同的方法签名。请尝试更新JAR以使用Spring提供的方法签名,看看是否有帮助。

我有一个更简单的设置,使用上下文配置,如下所示:

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());
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@上下文配置
公共类MyTest{
@自动连线
私人验证器;
@试验
公共void testMyForm(){
最终MyForm表单=新MyForm();
.
.
最终设置冲突=validator.validate(表单);
assertTrue(inflictions.isEmpty());
}
}
mytestcontext.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>


您好,我调查了您的建议,但没有发现版本不一致。我已经用我正在使用的版本更新了这个问题。我可以补充一点,在控制器中使用以下代码是有效的:@Valid TemplateFormBean fb,BindingResult。因此,我相信我在以编程方式使用此选项时缺少一些配置。我得到了相同的错误running WebSphere Application Server 7已经绑定了JPA 1.0,并且正在破坏hibernates JPA 2.0的实现。因此,我猜想您的类路径中的某个地方有JPA 1.0类。我尝试了您的建议,但仍然得到了原始错误:
java.lang.AbstractMethodError:javax/persistence/spi/PersistenceProvider.getProviderUtil()Ljavax/persistence/spi/ProviderUtil;位于javax.persistence.persistence$1.isLoaded(persistence.java:78)
。非常奇怪,因为它在部署时可以完美地工作,正如@duffymo所说,它看起来确实像是版本不一致或缺少服务器JAR,但我找不到错误。我最近将所有Hibernate验证JAR更新为4.2.0 final,将Hibernate JPA 2 API更新为1.0.1 final。仍然没有成功。我在另一个项目上又回到了这个特定的问题,一个这一次,它在测试用例中注入了一个验证器,而不是验证器工厂,从而完美地工作了。我基本上让Spring负责加载提供程序。所以这个答案对我来说是正确的。我使用的是Hibernate验证器5.0.1和Spring 3.1.x。
@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());
    }
}
<?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>