Skip to content
Snippets Groups Projects
Commit 6a5fa0d9 authored by Jitka Viceníková's avatar Jitka Viceníková
Browse files

Merge branch 'test-service-fix' into 'develop'

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

See merge request !35
parents 4f745128 944e5b57
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 { ...@@ -52,7 +52,7 @@ class CarComponentServiceTest {
void updateNonExistingComponent() { void updateNonExistingComponent() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.update(Long.MAX_VALUE, null); carComponentService.update(Long.MAX_VALUE, null);
...@@ -72,7 +72,7 @@ class CarComponentServiceTest { ...@@ -72,7 +72,7 @@ class CarComponentServiceTest {
void findNonExistingComponentById() { void findNonExistingComponentById() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.findById(Long.MAX_VALUE); carComponentService.findById(Long.MAX_VALUE);
...@@ -117,7 +117,7 @@ class CarComponentServiceTest { ...@@ -117,7 +117,7 @@ class CarComponentServiceTest {
void deleteNonExisting() { void deleteNonExisting() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
carComponentService.delete(Long.MAX_VALUE); carComponentService.delete(Long.MAX_VALUE);
......
...@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; ...@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
...@@ -58,7 +59,7 @@ class DepartmentServiceTest { ...@@ -58,7 +59,7 @@ class DepartmentServiceTest {
@Test @Test
void addNonExistingEngineer() { 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)); when(departmentRepository.findById(emptyDepartment.getId())).thenReturn(java.util.Optional.of(emptyDepartment));
assertThrows(ResourceNotFoundException.class, () -> service.addEngineer(emptyDepartment.getId(), 1L)); assertThrows(ResourceNotFoundException.class, () -> service.addEngineer(emptyDepartment.getId(), 1L));
......
...@@ -57,7 +57,7 @@ class DriverServiceTest { ...@@ -57,7 +57,7 @@ class DriverServiceTest {
void updateNonExistingDriver() { void updateNonExistingDriver() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
driverService.update(Long.MAX_VALUE, null); driverService.update(Long.MAX_VALUE, null);
...@@ -77,7 +77,7 @@ class DriverServiceTest { ...@@ -77,7 +77,7 @@ class DriverServiceTest {
void findNonExistingDriverById() { void findNonExistingDriverById() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
driverService.findById(Long.MAX_VALUE); driverService.findById(Long.MAX_VALUE);
...@@ -110,7 +110,7 @@ class DriverServiceTest { ...@@ -110,7 +110,7 @@ class DriverServiceTest {
void deleteNonExisting() { void deleteNonExisting() {
var exceptionToThrow = new ResourceNotFoundException(CarComponent.class, Long.MAX_VALUE); 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(), () -> { assertThrows(exceptionToThrow.getClass(), () -> {
driverService.delete(Long.MAX_VALUE); driverService.delete(Long.MAX_VALUE);
......
...@@ -35,7 +35,7 @@ class EngineerServiceTest { ...@@ -35,7 +35,7 @@ class EngineerServiceTest {
@Test @Test
void findNonExistingEngineerById() { 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)); 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