deleteById (existente, não existente e dependente)
Verificando delete (service)
Atualizado
Verificando delete (service)
Atualizado
@Test
public void deleteShouldDoNothingWhenIdExists() {
Assertions.assertDoesNotThrow(() -> {
productService.delete(existingId);
});
//serve para saber se o método do repository foi chamado alguma vez na ação acima. Recomenda-se fazer isso
Mockito.verify(productRepository, Mockito.times(1)).deleteById(existingId);
//o spring não lança exceção quando chama o delete.
}@Test
public void deleteShouldThrowResourceNotFoundExceptionWhenIdDoesntExist() {
Assertions.assertThrows(ResourceNotFoundException.class, () -> {
productService.delete(nonExistingId);
});
}@Test
public void deleteShouldThrowDatabaseExceptionWhenDependentId() {
Assertions.assertThrows(DatabaseException.class, () -> {
productService.delete(dependentId);
});
}