Skip to content
Snippets Groups Projects
Commit 944e5b57 authored by Alžbeta Hajná's avatar Alžbeta Hajná
Browse files

fix(Service tests): mocked repository return Optional.empty() instead of throwing exception

parent 4f745128
No related branches found
No related tags found
2 merge requests!54Merge develop into main,!35fix(Service tests): mocked repository return Optional.empty() instead of throwing exception
Pipeline #
......@@ -52,7 +52,7 @@ class CarComponentServiceTest {
void updateNonExistingComponent() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.update(Long.MAX_VALUE, null);
......@@ -72,7 +72,7 @@ class CarComponentServiceTest {
void findNonExistingComponentById() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.findById(Long.MAX_VALUE);
......@@ -117,7 +117,7 @@ class CarComponentServiceTest {
void deleteNonExisting() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(carComponentRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.delete(Long.MAX_VALUE);
......
......@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.HashSet;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
......@@ -58,7 +59,7 @@ class DepartmentServiceTest {
@Test
void addNonExistingEngineer() {
when(engineerRepository.findById(1L)).thenThrow(new ResourceNotFoundException(Engineer.class, 1L));
when(engineerRepository.findById(1L)).thenReturn(Optional.empty());
when(departmentRepository.findById(emptyDepartment.getId())).thenReturn(java.util.Optional.of(emptyDepartment));
assertThrows(ResourceNotFoundException.class, () -> service.addEngineer(emptyDepartment.getId(), 1L));
......
......@@ -57,7 +57,7 @@ class DriverServiceTest {
void updateNonExistingDriver() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(driverRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(driverRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
driverService.update(Long.MAX_VALUE, null);
......@@ -77,7 +77,7 @@ class DriverServiceTest {
void findNonExistingDriverById() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(driverRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(driverRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
driverService.findById(Long.MAX_VALUE);
......@@ -110,7 +110,7 @@ class DriverServiceTest {
void deleteNonExisting() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE);
when(driverRepository.findById(Long.MAX_VALUE)).thenThrow(exceptionToThrow);
when(driverRepository.findById(Long.MAX_VALUE)).thenReturn(Optional.empty());
assertThrows(exceptionToThrow.getClass(), () -> {
driverService.delete(Long.MAX_VALUE);
......
......@@ -35,7 +35,7 @@ class EngineerServiceTest {
@Test
void findNonExistingEngineerById() {
when(engineerRepository.findById(1L)).thenThrow(new ResourceNotFoundException(Engineer.class, 1L));
when(engineerRepository.findById(1L)).thenReturn(Optional.empty());
assertThrows(ResourceNotFoundException.class, () -> service.findById(1L));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment