我想编写有关修订的测试。在控制台中,我看到 Hibernate 的更新调用,但没有插入 AUD 表。
测试方法:
@DataJpaTest
class JPAHistoryTest {
@Test
public void test() {
def entity = // create Entity
def entity2 = // create same Entity
entity2.setPrice(entity2.getPrice() + 10)
entity2.setLastUpdate(entity2.lastUpdate.plusSeconds(10))
service.save(entity)
service.save(entity2)
repository.flush() // Hibernate updates changes
assert repository.findRevisions(entity.id).content.empty == false // FAIL!
}
}
我的实体看起来像:
@Entity
@Audited
class Entity {
@Id @GeneratedValue Long id
@Column(nullable = false) BigDecimal price
}
非常感谢。
最佳答案
我发现我保留了 @DataJpaTest
并添加了 @Transactional(propagation = NOT_SUPPORTED)
以确保测试方法不会启动事务。因为如果它们在事务中运行,那么当测试关闭事务时,envers 历史条目将被写入。
@RunWith(SpringRunner)
@DataJpaTest
@Transactional(propagation = NOT_SUPPORTED)
class JPAHistoryTest {
@After
void after() {
repository.deleteAll()
}
@Test
public void testTwoInsert() {
def entity1 = // ...
def entity2 = // ...
sut.save(entity1 )
sut.save(entity2 )
assert repository.findRevisions(entity1.id).content.size() == 1
assert repository.findRevisions(entity2.id).content.size() == 1
}
}
关于hibernate - 测试 Hibernate Envers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359451/