开发者

Force 'reload' of JPA EntityManager in integration test

开发者 https://www.devze.com 2023-03-22 18:37 出处:网络
In a Spring-enabled integration test, I need to force the EntityManager to re-read from the database.

In a Spring-enabled integration test, I need to force the EntityManager to re-read from the database.

@Test
@DataSet("/xml/dbunit/truncate-tables.xml")
public void createTerminalFromQuery() {

    // there should be zero terminals in the empty database
    Assert.assertEquals(0, terminalService.countTerminals());

    // makes remote REST call updating database outside the application's EntityManager
    HttpEntity<QueryResponse> result = simulateRequest("query")开发者_JAVA技巧;

    // attempts to force re-read of updated database (unsuccessfully)
    entityManagerService.getEntityManager().flush();

    // there should be exactly one Terminal in the database
    Assert.assertTrue(terminalService.existsTerminalBySerialNumber(EXISTING_TERMINAL_SERIAL_NUMBER));

}

It has been verified that the Terminal is created and exists in the database. Despite this, the second assertion fails. When the first assertion is commented out, the second one is OK.

The test framework is Unitils/DBUnit and injecting an EntityManager via @PersistenceContext is difficult, as the required package unitils-orm depends on Spring 2.5 and JPA 1.0 leading to other problems.

Instead, I have created the EntityManagerService and verified that it indeed uses the same EntityManager as the TerminalService.

I have tried EntityManager.clear() and flush() and evicting the Cache from the EntityManagerFactory - but nothing seems to have any effect.

Any suggestions?


It's not clear how do you manage transactions. Since clear() doesn't help, perhaps both checks are executed inside the same transaction that doesn't see result of the transaction committed by the REST service.

Try to execute checks in different transactions.

0

精彩评论

暂无评论...
验证码 换一张
取 消