Spring启动JPA不会在单个测试范围内持续更改

时间:2018-05-23 13:40:22

标签: java spring hibernate spring-boot jpa

我正在使用带有spring-boot-starter-data-jpa的Spring Boot 2.0.1。

在数据库中我有3条记录。我进行了这样的测试:

@Test
public void shouldDeleteByName() {
    service.deleteOneByName("SOME NAME"); // Out of 3 records 1 was deleted
    List<Customer> customers = service.selectAll();
    assertThat(customers).hasSize(2); // FAILS
assertThat(customers).extracting("name").doesNotContain("SOME NAME"); // FAILS
}

没有错误,我看到交易已经提交。

似乎交易要么在selectAll之前还原,selectAll还没有看到更改。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

好吧,它似乎解决了我的问题:

  1. 我将测试数据库配置事务管理器从JpaTransactionManager更改为DataSourceTransactionManager,最后发布了示例。

  2. 我将我的服务注释为@Transactional。

  3. 我将我的测试注释为@Transactional(没有这个工作,但更改是永久性的)。测试首次执行,但第二次失败。

  4. 第一步的代码工作一个

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }