【问题标题】:Spring CrudRepository deleteAll() does nothingSpring CrudRepository deleteAll() 什么都不做
【发布时间】:2019-07-20 08:24:38
【问题描述】:

当我尝试通过 Spring 的 CrudRepository 从我的数据库中删除所有记录时测试控制器类,但似乎什么也没发生。默认情况下似乎不刷新。

我从来没有在浏览器中的真正控制器调用中尝试过这个存储库,只是使用 Junit 测试,但我认为它可以正常工作! :)

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
public class CostumerControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private CostumerService costumerService;

    private Costumer costumer;

    @Before
    public void before() {
        this.costumer = this.exampleBuilder();
        costumerService.saveAll(this.costumer);
    }

    @After
    public void after() {
        costumerService.deleteAll();
    }

    @Test
    public void Should_ReturnStandardError_When_NotFoundById() throws Exception {
        //implementation
    }


private Costumer exampleBuilder() {

    Costumer costumer = new Costumer("Test", "Test", "Test", CostumerType.LEGAL_PERSON);
    State state = new State("Example State");
    City city = new City("Example Sity", state);
    Address address = new Address("Example Address",
            "Example Address", "Example Address",
            "Example Address", city, costumer);

    costumer.getAddresses().add(address);

    return costumer;
}

}

@Service
@Transactional
public class CostumerService {

    @Autowired
    private CostumerRepository repository;

    public void deleteAll() {
        repository.deleteAll();
    }

   //another methods

}

扩展 CrudRepository 的存储库

@Repository
public interface CostumerRepository extends CrudRepository<Costumer, Integer> {

}

启用后根据@TheCoder评论显示sqlhibernate.show_sql=true,结果为:

deleteAll() 上的@After

@After
public void after() {
    costumerService.deleteAll();
}

输出sql:

2019-02-27 06:07:06 - HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    select
        costumer0_.id as id1_3_,
        costumer0_.cpf_cnpj as cpf_cnpj2_3_,
        costumer0_.email as email3_3_,
        costumer0_.name as name4_3_,
        costumer0_.type as type5_3_ 
    from
        costumers costumer0_

@AfterTransaction 上的deteleAll() 输出 sql 包括删除查询。

@AfterTransaction
    public void afterTransatcion(){
        // List<Costumer> costumers = costumerService.findAll();
        costumerService.deleteAll();
    }


Hibernate: 
    select
        costumer0_.id as id1_3_,
        costumer0_.cpf_cnpj as cpf_cnpj2_3_,
        costumer0_.email as email3_3_,
        costumer0_.name as name4_3_,
        costumer0_.type as type5_3_ 
    from
        costumers costumer0_
Hibernate: 
    delete 
    from
        phones 
    where
        costumer_id=?
Hibernate: 
    delete 
    from
        costumers 
    where
        id=?

【问题讨论】:

  • 您的测试是连接到真实数据库,还是连接到内存数据库?
  • 连接到一个真实的MySql数据库。调用deleteAll()方法后,数据仍然存在于数据库中。
  • 这是测试的自然行为。测试完成后,测试所做的所有更改都将回滚。如果您不想要这种默认行为,可以在测试中使用 @Rollback(false)
  • 你的数据库类型是 InnoDB 吗?这可能会有所帮助stackoverflow.com/questions/2827770/…
  • 欢迎您....

标签: java spring-boot spring-data-jpa integration-testing


【解决方案1】:

在我的 junit 中,在 @BeforeEach 中添加 repository.deleteAllInBatch() 后,方法对我有用。仅在执行 junit 期间遇到此问题。

@BeforeEach
public void config()
{
   repository.deleteAllInBatch()
}

【讨论】:

    【解决方案2】:

    事务关闭时,数据将从数据库中删除。这应该只发生在测试方法的最后。

    但是,由于您使用的是事务性测试,所有事务将在测试后自动回滚。

    默认情况下,测试完成后测试事务会自动回滚;但是,事务提交和回滚行为可以通过 @Commit 和 @Rollback 注释以声明方式配置

    【讨论】:

    • 但是即使调用deleteAll(),数据库上的记录也不会被删除
    • 仅当您从测试中调用该记录时。删除将在测试结束时回滚。将@Rollback(false) 添加到您的测试中。
    • 对不起。不适用于@Rollback(false)。不适用于@Commit。不能同时使用@Rollback(false)@Commit!在@After 测试调用deleteAll() 后,数据仍在数据库中
    • 它适用于@AfterTransaction public void afterTransatcion(){ costumerService.deleteAll(); },但我不知道考虑到默认回滚行为是否有意义
    猜你喜欢
    • 2017-11-11
    • 2021-03-07
    • 2012-06-30
    • 2016-07-02
    • 1970-01-01
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    相关资源
    最近更新 更多