【发布时间】:2011-07-22 11:59:42
【问题描述】:
在启用 Spring 的集成测试中,我需要强制 EntityManager 从数据库中重新读取。
@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");
// 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));
}
已验证终端已创建并存在于数据库中。尽管如此,第二个断言失败了。当第一个断言被注释掉后,第二个就OK了。
测试框架是 Unitils/DBUnit,通过 @PersistenceContext 注入 EntityManager 很困难,因为所需的包 unitils-orm 依赖于 Spring 2.5 和 JPA 1.0 导致其他问题。
相反,我创建了 EntityManagerService 并验证它确实使用与 TerminalService 相同的 EntityManager。
我已经尝试过 EntityManager.clear() 和 flush() 并从 EntityManagerFactory 中逐出缓存 - 但似乎没有任何效果。
有什么建议吗?
【问题讨论】:
标签: jpa entitymanager