【发布时间】:2017-11-02 08:48:59
【问题描述】:
在jhipster sample spring boot application (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);
}
我想通过执行新的获取并检查 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 项目开始我的项目。
【问题讨论】:
标签: java rest spring-boot spring-data-jpa jhipster