Skip to content

Commit

Permalink
Merge pull request #37 from nastiausenko/refactor-user-servise-tests
Browse files Browse the repository at this point in the history
refactor UserServiceTest
  • Loading branch information
IvanShalaev1990 authored Apr 17, 2024
2 parents 8cb2688 + ebf6341 commit 427d916
Showing 1 changed file with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -36,7 +36,7 @@ class UserServiceTest {
*/
@BeforeEach
void setUp() {
user = User.builder()
user = User.builder()
.id(UUID.fromString("84991c79-f6a9-4b7b-b1b4-0d66c0b92c81"))
.email("[email protected]")
.password("password1")
Expand All @@ -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.
*/
Expand All @@ -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());
}

Expand All @@ -87,9 +105,8 @@ void updateByEmailDynamicallyTest() {
*/
@Test
void updateByEmailDynamicallyIsNullTest() {
assertThrows(NullEmailException.class, () -> {
userService.updateByEmailDynamically(user, null);
});
assertThatThrownBy(() -> userService.updateByEmailDynamically(user, null))
.isInstanceOf(NullEmailException.class);
}

/**
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand All @@ -137,7 +172,8 @@ void findByEmailNotFoundTest() {
String nonExistentUserEmail = "[email protected]";

when(userRepository.findByEmail(nonExistentUserEmail)).thenReturn(Optional.empty());
assertThrows(NoUserFoundByEmailException.class, () -> userService.findByEmail(nonExistentUserEmail));
assertThatThrownBy(() -> userService.findByEmail(nonExistentUserEmail))
.isInstanceOf(NoUserFoundByEmailException.class);
}

/**
Expand All @@ -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);
}
}

0 comments on commit 427d916

Please sign in to comment.