2

我陷入了一个古玩错误。

我有一个小的 DBUnit 测试:

@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) 预填充的条目。之后,我想通过 Spring Data CrudRepository 在嵌入式 H2 内存数据库中保存两个条目。

此数据源由 HibernateJpaVendorAdapter 和适配器本身包装在 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>

如果我运行上面的单元测试,我会收到以下错误:

21:21:55,808 DEBUG JpaTransactionManager:594 - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@f6b9e9] after transaction
21:21:55,808 DEBUG EntityManagerFactoryUtils:338 - Closing JPA EntityManager
21:21:55,809  WARN TestContextManager:397 - Caught exception while allowing TestExecutionListener [com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@4c4b3e] to process 'after' execution for test: method [public void de.axa.chronik.persistence.repository.KontaktrolleRepositoryIntegrationTest.that_Kontaktrolle_Is_Inserted_Into_DB() throws java.lang.Exception], instance [de.axa.chronik.persistence.repository.KontaktrolleRepositoryIntegrationTest@be41d5], exception [null]
org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking
    at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:544)...............
...........
Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:167)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:209)
    ... 33 more
Caused by: org.h2.jdbc.JdbcSQLException: Das Objekt wurde bereits geschlossen
The object is already closed [90007-175]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)

因此,由于连接已关闭,后续保持实体的尝试失败。hibernate persist 和我忽略的使用 ob Spring DBUnit 之间是否存在现有影响?

任何帮助表示赞赏。提前谢谢了。

问候, 博多

4

2 回答 2

1

尝试改变你的TestExecutionListeners. 此设置对我有用:

@TestExecutionListeners({
    DependencyInjectionTestExecutionListener.class,
    DbUnitTestExecutionListener.class,
    TransactionalTestExecutionListener.class
})

不再有org.h2.jdbc.JdbcSQLException: The object is already closed [90007-175]错误,我的测试现在在事务中执行。但是当事务结束时(即在每个测试用例之后) ,在 DbUnit 中所做的更改@DatabaseSetup不会回滚。

于 2017-04-29T10:15:57.337 回答
0

当您手动配置测试执行侦听器时,您需要注册TransactionalTestExecutionListener,否则事务不会在测试级别启动。当存储库返回像您看到的那样的原因异常时,这将导致事务已经关闭。

于 2014-04-01T09:24:22.017 回答