我正在嘗試通過單元測驗來測驗以下方法:
@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(...)”中的條件所致。那么,我應該如何修改這個測驗,以便我測驗我的軟洗掉方法?
這是我的Company物體,從DeletableEntity:
//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;
}
更新:這是我編輯的方法的最后狀態:
@Test
public void test_Successful_Delete() {
// Arrange
final String name = "Company";
final Company company = new Company(name);
when(companyRepository.findByUuid(company.getUuid()))
.thenReturn(Optional.of(company));
doNothing().when(deletionService).delete(isA(Company.class));
when(companyRepository.save(isA(Company.class))).thenReturn(company);
// Act
companyService.delete(company.getUuid());
// Assert
verify(deletionService, times(1)).delete(isA(Company.class));
verify(companyRepository, times(1)).save(isA(Company.class));
}
@Test(expected = EntityNotFoundException.class)
public void test_Delete_thenThrowException() {
final String name = "Company";
final Company company = new Company(name);
when(companyRepository.findByUuid(company.getUuid())).thenReturn(Optional.empty());
companyService.delete(company.getUuid());
}
uj5u.com熱心網友回復:
如果你想測驗這樣的方法,你必須通過兩個測驗來完成:
companyRepository.save()如果companyRepository.findByUuid()回傳任何內容,則確實呼叫的測驗;- 測驗該方法
EntityNotFoundException在companyRepository.findByUuid()什么都不回傳時拋出。
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)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359938.html
