Я разрабатываю веб-приложение, используя Spring 3.1
, Hibernate 3
и Hibernate 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;
}
Я написал тестовый класс для тестирования операций CRUD в классе DAO. Этот класс работает без ошибок для допустимых данных.
Теперь я хочу редактировать существующий объект. Поэтому я меняю имя. Я указываю
неверное имя, потому что я хочу написать отрицательный контрольный пример.
// 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 (ожидается).
Я делаю это, но получаю следующее сообщение об ошибке:
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>
Что я делаю не так? Кто-нибудь может мне помочь?