在 jhipster 示例 Spring Boot 应用程序( https://github.com/jhipster/jhipster-sample-app )中,删除测试如下:
@Test
@Transactional
public void deleteBankAccount() throws Exception {
// Initialize the database
bankAccountRepository.saveAndFlush(bankAccount);
int databaseSizeBeforeDelete = bankAccountRepository.findAll().size();
// Get the bankAccount
restBankAccountMockMvc.perform(delete("/api/bank-accounts/{id}", bankAccount.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<BankAccount> bankAccountList = bankAccountRepository.findAll();
assertThat(bankAccountList).hasSize(databaseSizeBeforeDelete - 1);
}
我想通过执行新的 get 并检查 404 错误来进行进一步验证。但我有奇怪的结果(可能是由于交易效应)。 删除项目 #1 后,如果我在测试中执行此操作:
bankAccountRepository.findAll() // item 1 is not present => OK
bankAccountRepository.findOne(1) // item 1 is present => BAD
restBankAccountMockMvc.perform(get("/api/bank-accounts/{id}", bankAccount.getId())) // HTTP code 200 => BAD
我不明白为什么 findAll 返回一个没有删除项目的列表,而 findOne 返回删除的项目。
我怎样才能拥有这个?
bankAccountRepository.findOne(1) // null
restBankAccountMockMvc.perform(get("/api/bank-accounts/{id}", bankAccount.getId())) // HTTP code 404
注意:我没有从 jhipster 项目启动我的应用程序,我只是查看生成的代码以获得好的想法。所以我从一个空的 maven/spring-boot 项目开始了我的项目。
最佳答案
您的断言使用了错误的 ID,即您正在删除不是 1 的 bankAccount.getId()
,因为在测试设置中创建了 BankAccount。您还应该使用用于删除的相同 ID 进行搜索:bankAccount.getId()
。
关于java - 已删除的实体实例在 Spring boot 单元测试中仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47071008/