我有這個方法
public PropertyEntity sendToApprove(Long id) throws CheckedException, NotFoundException {
PropertyEntity entity = propertyDataService.findById(id, false);
NotFoundException.check(entity == null, "Property not found.");
if (entity.getStatus() == PropertyStatus.DRAFT) {
CheckedException.check(propertyDataService.validateProperty(entity), "Please
apply all mandatory fields!");
entity.setStatus(PropertyStatus.PENDING);
// not use save method to avoid status changing to draft
propertyDataService.saveAll(Collections.singletonList(entity));
propertyDataService.updateMinMaxPriceAndBedBath(entity.getId());
} else {
throw new CheckedException("Approve from this status is not supported");
}
return entity;
}
如果物體為空,則需要撰寫測驗,然后方法:發送到批準拋出例外 NotFound。到目前為止,我是這個測驗的新手,我已經完成了這個
@ExtendWith(MockitoExtension.class)
public class PropertyControllerSendToApproveTest {
private PropertyDataService propertyDataService = Mockito.mock(PropertyDataService.class);
private PropertyManager propertyManager = Mockito.mock(PropertyManager.class);
@BeforeEach
public void init() throws CheckedException, NotFoundException {
Mockito.when(propertyManager.sendToApprove(1L)).thenReturn(null);
}
@Test
@DisplayName("when property is null then not found exception")
public void testWhenPropertyIsNull() throws CheckedException, NotFoundException {
when(propertyManager.sendToApprove(anyLong()) == null)
.thenThrow(NotFoundException.class);
}
}
uj5u.com熱心網友回復:
@Test
@DisplayName("when property is null then not found exception")
public void testWhenPropertyIsNull() throws CheckedException, NotFoundException {
when(propertyManager.sendToApprove(anyLong()) == null)
.thenThrow(NotFoundException.class);
}
所以在測驗用例中會拋出一個例外,正如你在 thenThrow 方法中提到的那樣。您需要添加
@Test(expected = NotFoundException.class)
預期意味著測驗用例需要 NotFoundException 并且您的測驗拋出相同的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427115.html
上一篇:日軸上的Python繪圖類別
