繁体   English   中英

Java中删除方法的单元测试?

[英]Unit Test for delete method in Java?

我正在尝试通过单元测试来测试以下方法:

@Override
@Transactional
public void delete(UUID uuid) {
    final Company company = companyRepository.findByUuid(uuid)
            .orElseThrow(() -> new EntityNotFoundException(COMPANY_NAME));

    deletionService.delete(company);
    companyRepository.save(company);
}

这是 DeletionService 的实现:

@Override
public void delete(final DeletableEntity deletableEntity) {
    deletableEntity.setDeleted(true);
    deletableEntity.setDeleteDate(Instant.ofEpochMilli(clock.millis()));
    deletableEntity.setDeletedByUserUuid(currentUser.getUuid());
}

我创建了以下测试方法:

@Test(expected = EntityNotFoundException.class)
public void test_Delete() {
    final UUID uuid = UUID.randomUUID();
    final String name = "Company";
    final Company company = new Company(name);

    when(companyRepository.findByUuid(uuid)).thenReturn(Optional.of(company));

    companyService.delete(uuid);

    // I think this returns company instead of EntityNotFoundException 
    // due to the condition in "Mockito.when(...)"
    companyRepository.findByUuid(uuid);
}

调试器命中,直到companyRepository.findByUuid(uuid); 行没有任何问题,但执行此行不会引发错误。 我认为这是由于“Mockito.when(...)”中的条件造成的。 那么,我应该如何修改这个测试,以便我测试我的软删除方法?

这是从DeletableEntity扩展的我的Company实体:

//some annotation here
public class Company extends DeletableEntity  {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "company_gen")
    private long id;

    @Column(nullable = false)
    private String name;

    public Company(@Nonnull String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        return super.equals(other);
    }
}
@MappedSuperclass
public class DeletableEntity extends BaseEntity {

    private boolean deleted;

    private Instant deleteDate;

    private UUID deletedByUserUuid;
}

如果你想测试这样的方法,你必须通过两个测试来完成:

  1. 测试companyRepository.save()的确是被称为如果companyRepository.findByUuid()返回任何东西;
  2. 测试该方法是否在companyRepository.findByUuid()返回任何内容时抛出EntityNotFoundException

1. 将类似于以下内容:

@Test
public void test_Successful_Delete() {
    // Arrange
    final UUID uuid = UUID.randomUUID();
    final String name = "Company";
    final Company company = new Company(name);
    when(companyRepository.findByUuid(uuid)).thenReturn(Optional.of(company));
    when(deletionService.delete(isA(Company.class))).thenReturn(true; // I am assuming deletionService.delete() returns a boolean, if that is not the case you will need to adjust this
    when(companyRepository.save(isA(Company.class))).thenReturn(true; // I am assuming companyRepository.save() returns a boolean, if that is not the case you will need to adjust this

    // Act
    companyService.delete(uuid);

    // Assert
    verify(deletionService, times(1)).delete(isA(Company.class));
    verify(companyRepository, times(1)).save(isA(Company.class));
}

2. 将如下所示(类似于您的):

@Test
public void test_Unsuccessful_Delete() {
    // Arrange
    final UUID uuid = UUID.randomUUID();
    final String name = "Company";
    final Company company = new Company(name);
    when(companyRepository.findByUuid(uuid)).thenReturn(Optional.empty());

    try {
      // Act
      companyService.delete(uuid);
      Assert.fail("Exception not thrown");
    } catch (EntityNotFoundException e) {
      // Assert
      verify(deletionService, never()).delete(isA(Company.class));
      verify(companyRepository, never()).save(isA(Company.class));
    }
}

PS: assertThat是一个 AssertJ 方法( org.assertj.core.api.Assertions.assertThat )。

暂无
暂无

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

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