I've got this method
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;
}
Need to write test if entity is null then method: send to approve throws exception NotFound. I'm new in this testing so far I've done this
@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);
}
}
CodePudding user response:
@Test
@DisplayName("when property is null then not found exception")
public void testWhenPropertyIsNull() throws CheckedException, NotFoundException {
when(propertyManager.sendToApprove(anyLong()) == null)
.thenThrow(NotFoundException.class);
}
So in the test case would throw an exception as you have mentioned with the thenThrow method. You need to add
@Test(expected = NotFoundException.class)
expected means the test case expects a NotFoundException and your test throws the same