hibernate - 测试 Hibernate Envers

标签 hibernate testing junit hibernate-envers

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

相关文章:

java - 如何使用 Hibernate 映射 byte[] 属性?

java - 如何为少数测试类而不是所有测试类运行 cobertura 代码覆盖率?

java - 如何告诉 Hibernate 不要更新某些列

mysql - Dropwizard @UnitOfWork : Large number of SET autocommit=1/0; commit calls where there is no transaction to run

php - 如何在 Laravel 中测试 JSON 文件中存在的嵌套 JSON 数据?

java - Selenium Java,用于跨测试运行相同设置的类

gradle - 为什么@DisplayName 在 JUnit 5 中对我不起作用?

java - Mockito JUnit 测试抛出异常的方法

Hibernate Search + Infinispan + S3 -- 防止字母数字文件名

testing - 进行自动验收测试以测试字段是否保存到数据库是否重要?