如何编写异常测试
How to write test for exception
我有这个方法
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;
}
需要编写测试if entity is null then method: send to approve throws exception 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);
}
}
@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)
expected 表示测试用例需要 NotFoundException 而您的测试抛出相同的异常
我有这个方法
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;
}
需要编写测试if entity is null then method: send to approve throws exception 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);
}
}
@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)
expected 表示测试用例需要 NotFoundException 而您的测试抛出相同的异常