简体   繁体   English

JUnit 测试通过 id 删除实体失败

[英]JUnit test to delete an entity by id fails

I am trying to make a test for the method to delete an entity by id.我正在尝试对通过 id 删除实体的方法进行测试。 I've written the test for the case when an entity with the given id doesn't exist, but in case when the entity exists, I'm apparently doing something wrong.我已经为具有给定 id 的实体不存在的情况编写了测试,但如果实体存在,我显然做错了什么。 This is the delete method from the service.这是服务中的删除方法。 I'm using the deleteById() method of the JpaRepository .我正在使用JpaRepositorydeleteById()方法。

  public void deleteById(Integer id) {
        Optional<Agency> optionalAgency = repo.findAll().stream()
                .filter(agency -> agency.getId().equals(id))
                .findFirst();

        if (optionalAgency.isPresent()) {
            repo.deleteById(optionalAgency.get().getId());
        } else {
            throw new AgencyNotFoundException("Agency not found");
        }
    }

And this is my test:这是我的测试:

    @Mock
    private AgencyRepository agencyRepository;

    @Mock
    private AgencyMapper mapper;

    @InjectMocks
    private AgencyService agencyService;

    @Test
    void delete() {
        Agency agency = new Agency();

        when(agencyRepository.findById(1)).thenReturn(Optional.of(agency));

        agencyService.deleteById(1);

        verify(agencyRepository).deleteById(1);

    }

What assertion should I make in order to verify that the deletion was successful?为了验证删除是否成功,我应该做出什么断言? The way I've tried it it doesn't work, the result of the test is that an exception is thrown.我尝试过的方式不起作用,测试的结果是抛出异常。 I'm guessing that the exception is thrown because agencyRepository.findById((int) 1L);我猜测抛出异常是因为agencyRepository.findById((int) 1L); basically doesn't exist anymore, but I thought maybe there's a better way to verify the deletion, without searching for the deleted object.基本上不存在了,但我想也许有更好的方法来验证删除,而不用搜索已删除的 object。

repo is a mock, so calling deleteById on it won't actually delete anything. repo是一个模拟,所以在它上面调用deleteById实际上不会删除任何东西。 Instead, you could verify that calling the AgencyService 's deleteById actually calls AgencyRepository 's deleteById .相反,您可以验证调用AgencyServicedeleteById实际上调用了AgencyRepositorydeleteById

EDIT:编辑:
The repository's code uses findAll , not findById , so you need to make sure you mock that:存储库的代码使用findAll ,而不是findById ,因此您需要确保模拟:

@Test
void delete() {
    Agency agency = new Agency();
    agenct.setId((int) 1L);

    when(agencyRepository.findAll()).thenReturn(Collections.singletonList(agency));

    agencyService.deleteById((int) 1L);

    verify(agencyRepository).delteById((int) 1L);
}

Not the answer, but your method could be written something like this:不是答案,但你的方法可以写成这样:

 public void deleteById(Integer id) {
     Agency agency = repo.findById(id).orElseThrow(() -> throw new AgencyNotFoundException("Agency not found"));
     repo.deleteById(agency.getId());
 }

Because:因为:

  • Optional<Agency> optionalAgency = repo.findAll().stream().filter(agency -> agency.getId().equals(id)).findFirst(); is not efficient.效率不高。 What if you have thousands of agencies?如果你有成千上万的机构怎么办? Will you fetch all and filter through all of them just to find by id.您会获取所有内容并过滤所有内容,以便通过 id 查找。 You have that method in your repository already.您的存储库中已经有该方法。
  • if (optionalAgency.isPresent()) {} Optional.isPresent is not good practise. if (optionalAgency.isPresent()) {} Optional.isPresent 不是好习惯。 Read here and this answer阅读这里这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM