gpt4 book ai didi

hibernate - 测试 Hibernate Envers

转载 作者:行者123 更新时间:2023-11-28 21:22:29 26 4
gpt4 key购买 nike

我想编写有关修订的测试。在控制台中,我看到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com