Im使用spring 3.0.7、JUnit 4.11和DBUnit 2.4.9。我试图在保存未指定多对一关系(设置为null)的实体时测试trowing异常。以下是实体:
<class name="DistributionPoint" table="distribution_point">
<id name="id" unsaved-value="null" type="java.lang.Long">
<column name="id"></column>
<generator class="hilo"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="255"></column>
</property>
<property name="description" type="java.lang.String">
<column name="description" length="1024"></column>
</property>
<property name="image" type="binary">
<column name="image"></column>
</property>
<many-to-one name="site" class="Site">
<column name="siteId" not-null="true"></column>
</many-to-one>
</class>
我创建了以下测试:
@TransactionConfiguration(defaultRollback=false, transactionManager="transactionManager")
//@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/mysql-test-context.xml" })
public class MysqlDistributionPointTest extends AbstractDatabaseTest {
AbstractDatabaseTest扩展了AbstractTransactionalJUnit4SpringContextTests。
试验方法为:
@Test(expected=org.hibernate.PropertyValueException.class)
public void testSaveWithoutSite() {
// set up DP
DistributionPoint distributionPoint = new DistributionPoint();
distributionPoint.setName( "Test distribution point" );
distributionPoint.setDescription( "Test description" );
// save DP
this.distributionPointDao.save( distributionPoint );
}
但是当我做测试的时候
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:648)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:515)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:155)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
... 27 more
当我像这样将预期异常更改为DataIntegrityViolationException时
@Test(expected = DataIntegrityViolationException.class)
public void testSaveWithoutSite() {
DistributionPoint distributionPoint = new DistributionPoint();
distributionPoint.setName( "Test distribution point" );
distributionPoint.setDescription( "Test description" );
this.distributionPointDao.save( distributionPoint );
}
我得到以下堆栈跟踪
java.lang.Exception: Unexpected exception, expected<org.springframework.dao.DataIntegrityViolationException> but was<org.hibernate.PropertyValueException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665)
at com.hyperoptic.infinity.commons.dao.HbmGenericDaoBase.save(HbmGenericDaoBase.java:47)
at data.test.MysqlDistributionPointTest.saveWithoutSite(MysqlDistributionPointTest.java:95)
at data.test.MysqlDistributionPointTest.testSaveWithoutSite(MysqlDistributionPointTest.java:82)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 24 more
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:648)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:515)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.hyperoptic.infinity.crm.model.DistributionPoint.site
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:155)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
... 27 more
即使我像这样环绕save call,也会出现此异常:
@Test
public void testSaveWithoutSite() {
DistributionPoint distributionPoint = new DistributionPoint();
distributionPoint.setName( "Test distribution point" );
distributionPoint.setDescription( "Test description" );
try {
// save DP
this.distributionPointDao.save( distributionPoint );
} catch (Exception e) {
// TODO: handle exception
}
}
这让我很困惑。。有人有类似的问题吗?
使用测试(预期=DataIntegrityViolationException.class)
您必须使用最上面的异常。在您的预期中,您列出了原因,而不是最常见的异常。
好的,等一下。如果您在测试中使用了预期的,则测试中不应使用try/catch。或者,如果这样做,则应重新引发异常。
问题内容: 我正在尝试对其他人编写的代码进行JUnit测试,但是我无法弄清楚如何测试该异常,因为该异常似乎缺少类型。 我的JUnit代码段: 我知道我需要在测试的括号中加上期望的=(某种异常类型),但是对于异常类型我一无所知。 问题答案: 实际上,在JUnit 4.7中有一个替代方法是使用和 在测试用例中,您声明带有,并为其指定默认值。然后,在预期异常的测试中,将值替换为实际预期值。这样做的好处是
在插入记录数据库过程中,我正在与以下问题作斗争。 2018-08-12 16:06:26.874警告5482---[nio-8080-exec-2]O.H.Engine.jdbc.spi.sqlexceptionHelper:SQL错误:1364,SQLState:HY000 2018-08-12 16:06:26.875错误5482--[nio-8080-exec-2]O.H.Engine.jd
java.lang.AssertionError:预期测试将在org.junit.Assert.Fail(Assert.java:88)在org.junit.rules.ExpectedException.FailduetOmissingException(ExpectedException.java:263)在org.junit.rules.ExpectedException.Access$20
问题内容: 我有一个单元测试类,需要运行相当广泛的数据库,然后才能运行各个测试方法。这种设置需要很长时间:出于希望与手头问题无关的原因,我需要以编程方式填充数据库,而不是从SQL转储中填充数据库。 我的问题是拆卸。如何轻松回滚在数据库设置阶段所做的所有更改? 我目前正在使用Hibernate + Spring事务测试支持,因此我的各个测试方法都包含在事务中。 一种解决方案是在每种测试方法中进行数据
我无法解决这个问题。我得到了“线程中的异常”main”org.hibernate.LazyInitializationException:未能延迟初始化角色集合:org.sandy.domain.Location.items,没有会话或会话被关闭”我知道会话已关闭,但tx: notionation-drive@Transactional应该确保打开会话。它可以很好地与EAGER获取一起工作。哦,是
我是spring和hibernate的新手,我正在尝试从我的spring+hibernate应用程序与mysql数据库连接。但不幸的是,我得到了以下错误。试图修复此问题,但未得到正确的解决方案。 说明服务器遇到意外情况,无法完成请求。 例外情况 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.springfra