gpt4 book ai didi

javax.validation.ConstraintValidationException : validation failed for classes

转载 作者:搜寻专家 更新时间:2023-11-01 03:44:15 30 4
gpt4 key购买 nike

我正在开发一个 Web 应用程序,在后端使用 Spring 3.1Hibernate 3Hibernate Validator 4。我正在使用 JSR 303 进行验证。这是通过域类中的注释完成的。

   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;
}

我写了一个测试类来测试DAO类中的CRUD操作。此类针对有效数据运行时没有错误。现在我想编辑一个现有的对象。因此,我更改了名字。我指定一个名称无效,因为我想写一个否定的测试用例。

// 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.");

}

}

系统正确地引发了以下错误消息:javax.validation.ConstraintValidationException在这种情况下,必须指明预期的异常。在 JUnit 4 中,@Test(expected) 是可能的。

我这样做了,但我收到以下错误消息:

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)

这是我的 portal-test.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" 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>

我做错了什么?谁能帮帮我?

最佳答案

以下代码将在您的代码中打印验证错误,您可以从中找到问题所在

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());
}

关于javax.validation.ConstraintValidationException : validation failed for classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6342781/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com