开发者

javax.validation.ConstraintValidationException: validation failed for classes

I am developing a web application using Spring 3.1, Hibernate 3 and Hibernate Validator 4 in the backend. I'm using JSR 303 for the validation. This is done via annotations in the domain class.

   public class StaffMember implements Serializable {
       @NotNull
    @Size(max = 30)
    // All letters, spaces and hyphens are allowed
    @Pattern(regexp = "^[^0-9_.]+$", message = ("Es sind nur Buchstaben, Leerzeichen     
         und Bindestrich erlaubt."))
    private String firstname;
   }

I have written a test class for testing the CRUD operations in the DAO class. This class runs without errors for valid data. Now I want to edit an existing object. Therefore I change the first name. I specify an invalid name because I want to write a negative test case.

// Allows Spring to configure the test
@RunWith(SpringJUnit4ClassRunner.class)
// Define which configuration should be used
@ContextConfiguration(locations = { "classpath:portal-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback=false)
// All methods are transactional
@Transactional
public class StaffMemberDAOTest {

private StaffMember staffMember, nextStaffMember;

@Autowired
private StaffMemberDAO staffMemberDao;

@Autowired
private Validator validator;

@Test(expected = ConstraintViolationException.class)
@Transactional
@Rollback(true)
public void testUpdateStaffMemberWithInvalidData() {

    System.out.println("--- update a staffMember (with invalid data) ---");

    // check if database is empty
    Assert.assertEquals(0, staffMemberDao.getAllStaffMembers().size());

    // add staffMember
    createStaffMember();

    // validate
    Set<ConstraintViolation<StaffMember>> violations = validator
        .validate(staffMember);

    // object is valid
    if(violations.size() == 0) {

        staffMemberDao.addStaffMember(staffMember);

    } else {

        System.out.println("Object is not valid.");

    }

    // get staffMember
    StaffMember staffMemberExpected = staffMemberDao.getStaffMember(staffMember.getStaffMemberID());

    // check data
    Assert.assertEquals(staffMember, staffMemberExpected);

    // edit data
    staffMemberExpected.setFirstname("George No 1");

    // validate
    Set<ConstraintViolation<StaffMember>> violationsUpdate = validator
    .validate(staffMemberExpected);

    // object is valid
    if(violationsUpdate.size() == 0) {

        staffMemberDao.editStaffMember(staffMemberExpected);

    } else {

        System.out.println("Object is not valid.");

    }

}

The system rightly raises the following error message: javax.validation.ConstraintValidationException In this case the expected exception must to be indicated. In JUnit 4 is this possible about @Test(expected).

I do this but i get the following error message:

java.lang.AssertionError: Expected exception: javax.validation.ConstraintViolationException
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

javax.validation.ConstraintViolationException: validation failed for classes [de.softwareinmotion.portal.domain.StaffMember] during update time for groups [javax.validation.groups.Default, ]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:155)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:102)
    at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:235)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:86)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at de.softwareinmotion.portal.persistence.StaffMemberDAO.getAllStaffMembers(StaffMemberDAO.java:37)
    at de.softwareinmotion.portal.persistence.StaffMemberDAO$$FastClassByCGLIB$$ae2a690a.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
    at de.softwareinmotion.portal.persistence.StaffMemberDAO$$EnhancerByCGLIB$$8fbc7a01.getAllStaffMembers(<generated>)
    at de.softwareinmotion.portal.persistence.StaffMemberDAOTest.tearDown(StaffMemberDAOTest.java:542)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.开发者_高级运维java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Here is my portal-test.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-3.0.xsd">

   <context:property-placeholder location="classpath:jdbcTest.properties" />
   <context:annotation-config/>
   <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close">
        <property name="driverClassName" value="${db.driverClass}" />
        <property name="url" value="${db.jdbcUrl}" />
        <property name="username" value="${db.user}" />
        <property name="password" value="${db.password}" />
    </bean>

    <!--  Hibernate config -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
          <list>
                <!-- Each domain class must be listed here -->
                <value>de.softwareinmotion.portal.domain.StaffMember</value>
          </list>
        </property>
        <property name="hibernateProperties">
          <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
          </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 

    <!-- Necessary for validation -->
    <bean name="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <!-- <property name="validationMessageSource">
            <ref bean="resourceBundleLocator"/>
        </property>   -->
    </bean>

    <!-- <bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/validation-messages" />
    </bean>   -->

    <!-- Each DAO object must be declared here! -->
    <bean id="staffMemberDao" class="de.softwareinmotion.portal.persistence.StaffMemberDAO">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

What I'm doing wrong? Can anybody help me?


Following code will print the validation errors in your code from which you can find whee the problem lies

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Notice>> constraintViolations = validator.validate(notice);
        for (ConstraintViolation cv : constraintViolations) {
            System.out.println("ValidatationConstraint: " + cv.getConstraintDescriptor().getAnnotation());
            System.out.println("ValidatationConstraint: " + cv.getConstraintDescriptor());
            System.out.println("ValidatationConstraint: " + cv.getMessageTemplate());
            System.out.println("ValidatationConstraint: " + cv.getInvalidValue());
            System.out.println("ValidatationConstraint: " + cv.getLeafBean());
            System.out.println("ValidatationConstraint: " + cv.getRootBeanClass());
            System.out.println("ValidatationConstraint: " + cv.getPropertyPath().toString());
            System.out.println("ValidatationConstraint: " + cv.getMessage());
        }


Hibernate throws the validation exception at flush time. Hibernate does not flush until your next query, until the transaction is committed, or until you manually ask for a flush.

The test is complaining because it didn't see the validation exception. Then, during the tear down of your test, you issue a query which causes a flush which causes the validation exception to print.

You could try manually flushing at the end of your test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜