diff --git a/src/test/java/com/linkurlshorter/urlshortener/user/UserServiceTest.java b/src/test/java/com/linkurlshorter/urlshortener/user/UserServiceTest.java index 24ed572..5bb3aea 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/user/UserServiceTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/user/UserServiceTest.java @@ -5,13 +5,13 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.util.Optional; import java.util.UUID; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -36,7 +36,7 @@ class UserServiceTest { */ @BeforeEach void setUp() { - user = User.builder() + user = User.builder() .id(UUID.fromString("84991c79-f6a9-4b7b-b1b4-0d66c0b92c81")) .email("test1@gmail.com") .password("password1") @@ -49,27 +49,45 @@ void setUp() { */ @Test void saveTest() { - when(userRepository.save(Mockito.any(User.class))).thenReturn(user); + when(userRepository.save(any(User.class))).thenReturn(user); User savedUser = userService.save(user); - assertNotNull(savedUser); - assertEquals(user, savedUser); + assertThat(savedUser).isNotNull().isEqualTo(user); verify(userRepository, times(1)).save(user); } + /** + * Test case for the {@link UserService#save(User)} method when the user is null. + */ + @Test + void saveNullTest() { + when(userRepository.save(any(User.class))).thenReturn(null); + assertThatThrownBy(() -> userService.save(null)) + .isInstanceOf(NullUserPropertyException.class); + } + /** * Test case for the {@link UserService#update(User)} method. */ @Test void updateTest() { - when(userRepository.save(Mockito.any(User.class))).thenReturn(user); + when(userRepository.save(any(User.class))).thenReturn(user); User updatedUser = userService.update(user); - assertNotNull(updatedUser); - assertEquals(user, updatedUser); + assertThat(updatedUser).isNotNull().isEqualTo(user); verify(userRepository, times(1)).save(user); } + /** + * Test case for the {@link UserService#update(User)} method when the user is null. + */ + @Test + void updateNullTest() { + when(userRepository.save(any(User.class))).thenReturn(null); + assertThatThrownBy(() -> userService.update(null)) + .isInstanceOf(NullUserPropertyException.class); + } + /** * Test case for the {@link UserService#updateByEmailDynamically(User, String)} method. */ @@ -78,7 +96,7 @@ void updateByEmailDynamicallyTest() { when(userRepository.updateUserByEmailDynamically(any(User.class), eq(user.getEmail()))).thenReturn(1); int updatedRecord = userService.updateByEmailDynamically(user, user.getEmail()); - assertEquals(1, updatedRecord); + assertThat(updatedRecord).isEqualTo(1); verify(userRepository, times(1)).updateUserByEmailDynamically(user, user.getEmail()); } @@ -87,9 +105,8 @@ void updateByEmailDynamicallyTest() { */ @Test void updateByEmailDynamicallyIsNullTest() { - assertThrows(NullEmailException.class, () -> { - userService.updateByEmailDynamically(user, null); - }); + assertThatThrownBy(() -> userService.updateByEmailDynamically(user, null)) + .isInstanceOf(NullEmailException.class); } /** @@ -100,10 +117,18 @@ void findByIdTest() { when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); User foundUser = userService.findById(user.getId()); - assertEquals(user, foundUser); + assertThat(foundUser).isNotNull().isEqualTo(user); verify(userRepository, times(1)).findById(user.getId()); } + /** + * Test case for the {@link UserService#findById(UUID)} method when the provided id is null. + */ + @Test + void findByIdNullTest() { + assertThatThrownBy(() -> userService.findById(null)) + .isInstanceOf(NullUserPropertyException.class); + } /** * Test case for the {@link UserService#findById(UUID)} method * when the user with provided id does not exist. @@ -113,7 +138,8 @@ void findByIdNotFoundTest() { UUID nonExistentUserId = UUID.randomUUID(); when(userRepository.findById(nonExistentUserId)).thenReturn(Optional.empty()); - assertThrows(NoUserFoundByIdException.class, () -> userService.findById(nonExistentUserId)); + assertThatThrownBy(() -> userService.findById(nonExistentUserId)) + .isInstanceOf(NoUserFoundByIdException.class); } /** @@ -124,10 +150,19 @@ void findByEmailTest() { when(userRepository.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); User foundUser = userService.findByEmail(user.getEmail()); - assertEquals(user, foundUser); + assertThat(foundUser).isNotNull().isEqualTo(user); verify(userRepository, times(1)).findByEmail(user.getEmail()); } + /** + * Test case for the {@link UserService#findByEmail(String)} method when the provided email is null. + */ + @Test + void findByEmailNullTest() { + assertThatThrownBy(() -> userService.findByEmail(null)) + .isInstanceOf(NullUserPropertyException.class); + } + /** * Test case for the {@link UserService#findByEmail(String)} method * when the user with provided email does not exist. @@ -137,7 +172,8 @@ void findByEmailNotFoundTest() { String nonExistentUserEmail = "nonexistent@gmail.com"; when(userRepository.findByEmail(nonExistentUserEmail)).thenReturn(Optional.empty()); - assertThrows(NoUserFoundByEmailException.class, () -> userService.findByEmail(nonExistentUserEmail)); + assertThatThrownBy(() -> userService.findByEmail(nonExistentUserEmail)) + .isInstanceOf(NoUserFoundByEmailException.class); } /** @@ -146,6 +182,15 @@ void findByEmailNotFoundTest() { @Test void deleteByIdTest() { userService.deleteById(user.getId()); - verify(userRepository, Mockito.times(1)).deleteById(user.getId()); + verify(userRepository, times(1)).deleteById(user.getId()); + } + + /** + * Test case for the {@link UserService#deleteById(UUID)} method when the provided id is null. + */ + @Test + void deleteByIdNullTest() { + assertThatThrownBy(() -> userService.deleteById(null)) + .isInstanceOf(NullUserPropertyException.class); } }