[英]Unit Test for delete method in Java?
I am trying to test the following method by a Unit Test:我正在尝试通过单元测试来测试以下方法:
@Override
@Transactional
public void delete(UUID uuid) {
final Company company = companyRepository.findByUuid(uuid)
.orElseThrow(() -> new EntityNotFoundException(COMPANY_NAME));
deletionService.delete(company);
companyRepository.save(company);
}
Here is the DeletionService implementation:这是 DeletionService 的实现:
@Override
public void delete(final DeletableEntity deletableEntity) {
deletableEntity.setDeleted(true);
deletableEntity.setDeleteDate(Instant.ofEpochMilli(clock.millis()));
deletableEntity.setDeletedByUserUuid(currentUser.getUuid());
}
I created the following test method:我创建了以下测试方法:
@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);
}
The debugger hits until companyRepository.findByUuid(uuid);
调试器命中,直到
companyRepository.findByUuid(uuid);
line without any problem, but executing this line does not throw error.行没有任何问题,但执行此行不会引发错误。 I think this is due to the condition in "Mockito.when(...)".
我认为这是由于“Mockito.when(...)”中的条件造成的。 So, how should I modify this test so that I test my soft delete method?
那么,我应该如何修改这个测试,以便我测试我的软删除方法?
Here is my Company
entity that extends from DeletableEntity
:这是从
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;
}
If you want to test such a method you have to do it with two tests:如果你想测试这样的方法,你必须通过两个测试来完成:
companyRepository.save()
is indeed called if companyRepository.findByUuid()
returns anything;companyRepository.save()
的确是被称为如果companyRepository.findByUuid()
返回任何东西;EntityNotFoundException
when companyRepository.findByUuid()
returns nothing.companyRepository.findByUuid()
返回任何内容时抛出EntityNotFoundException
。 The 1. would be something like the following: 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));
}
The 2. would look as follows (similar to yours): 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
is an AssertJ method ( org.assertj.core.api.Assertions.assertThat
). PS:
assertThat
是一个 AssertJ 方法( org.assertj.core.api.Assertions.assertThat
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.