Unit testing JSR-303 validation in Spring

三世轮回 提交于 2019-12-05 18:48:19

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!