使用Spring DBUnit、Spring数据和嵌入式H2数据库时发生回滚错误
我陷入了一个古玩错误中 我有一个小的DBUnit测试:使用Spring DBUnit、Spring数据和嵌入式H2数据库时发生回滚错误,spring,spring-data,h2,sqlexception,spring-test-dbunit,Spring,Spring Data,H2,Sqlexception,Spring Test Dbunit,我陷入了一个古玩错误中 我有一个小的DBUnit测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/testApplicationContext.xml") @Category(Integrationtest.class) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/testApplicationContext.xml")
@Category(Integrationtest.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class
})
@DatabaseSetup("classpath:db/wbSchluesselwertData.xml")
public class KontaktrolleRepositoryIntegrationTest {
@Autowired
KontaktrolleRepository kontaktrolleRepository;
@Autowired
KontaktBeteiligterRepository kontaktBeteiligterRepository;
@Autowired
WbSchluesselwertRepository wbSchluesselwertRepository;
........
........
@Test
@Transactional
public void that_Kontaktrolle_Is_Inserted_Into_DB() throws Exception {
WbSchluesselwert wbRolle1 = wbSchluesselwertRepository.findOne(new Long(11));
KontaktBeteiligter b1 = new KontaktBeteiligterBuilderAws(erzeugtAm).withName("Posteingang")
.withStandort("Cologne").withOrganization("private").build();
KontaktBeteiligter result = kontaktBeteiligterRepository.save(b1);
Kontaktrolle r1 = new KontaktrolleBuilder(erzeugtAm).withBeteiligter(result).withRollenartId(wbRolle1).build();
Kontaktrolle resultRolle = kontaktrolleRepository.save(r1);
assertNotNull(resultRolle);
assertNotNull(resultRolle.getKontaktrolleId());
assertThat(resultRolle.getKontaktrolleId(), greaterThan(0L));
}
没别的了。首先,我想搜索一个以DBUnit(wbSchluesselwertData.xml)作为前缀的条目。
之后,我想通过SpringDataCrudepository在嵌入式H2内存数据库中保存两个条目
此数据源由HibernateJavaEndorapter和适配器本身包装在LocalContainerEntityManagerFactoryBean中:
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="de.axa.chronik.persistence.domain" />
</bean>
因此,后续持久化实体的尝试失败,因为连接已关闭。
hibernate persist和我忽略的使用ob Spring DBUnit之间是否存在影响
感谢您的帮助。
非常感谢
问候,,
Bodo当您手动配置测试执行侦听器时,您需要注册
TransactionalTestExecutionListener
,否则事务不会在测试级别上启动。当存储库返回类似于您看到的原因异常时,这将导致事务已经关闭。尝试更改您的TestExecutionListeners
。此设置对我有效:
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class
})
没有
org.h2.jdbc.JdbcSQLException:对象已经关闭[90007-175]
错误,我的测试现在在事务内部执行但是在DbUnit的@DatabaseSetup
中所做的更改在事务结束时(即在每个测试用例之后)不会回滚。嗯,不幸的是,我根本没有得到你的答案。不幸的是,我根本没有得到你的答案。在我的applicationContext中,我将JpaTransactionManager设置为LocalContainerEntityManagerFactoryBean属性。在上面的测试用例中,我注册了TransactionDbUnitTestExecutionListener.class。我可以注册TransactionalTestExecutionListener.class和DbUnitTestExecutionListener.class来代替这个类,但效果相同。我还可以用Rollback(false)注释测试方法,但这也没有任何效果。我的内存数据库在testApplicationContext中声明:…将TransactionDbUnitTestExecutionListener.class
更改为TransactionTestExecutionListener.class
+DbUnitTestExecutionListener.class
有助于消除此错误。我现在可以确认我的测试是在事务内部执行的。但在@DatabaseSetup
中所做的更改在事务结束时(即在每个测试用例之后)不会回滚。这些东西迫使我进入精神不稳定的状态。。。只有在TransactionalTestExecutionListener
之前注册DbUnitTestExecutionListener
时,它才起作用。我收到相同的错误消息JdbcSQLException:在使用H2、Spring和Hibernate(JPA)的DbUnit测试中,对象已经关闭[90007-195]
。因此,我假设问题与Spring数据无关。如果我将TransactionDbUnitTestExecutionListener.class
更改为DbUnitTestExecutionListener.class
,错误会消失,但测试后不会回滚数据库更改。
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class
})