Skip to content

Commit

Permalink
fixed UserService Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-nowak authored and ogedo committed Apr 19, 2023
1 parent af72706 commit 12032ef
Showing 1 changed file with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,35 @@ void delete_method_should_not_throw_exception() {
Assertions.assertDoesNotThrow(() -> userService.removeById(fakeUser.getId()));
}

// @Test
// void save_method_should_return_saved_user_when_user_does_not_exist() {
// Mockito.when((clock.getZone())).thenReturn(NOW.getZone());
// Mockito.when((clock.instant())).thenReturn(NOW.toInstant());
// Mockito.when(userRepository.save(
// fakeUser.withPassword(
// encoder.encode(fakeUser.getPassword())
// )
// )).thenReturn(fakeUser);
//
// //when
// User savedUser = userService.save(fakeUser, "creatorId");
//
// //then
// Assertions.assertNotNull(savedUser);
// Assertions.assertEquals(fakeUser.getId(), savedUser.getId());
// Assertions.assertEquals(fakeUser.getEmail(), savedUser.getEmail());
// Assertions.assertEquals(fakeUser.getName(), savedUser.getName());
// Assertions.assertEquals(fakeUser.getPassword(), savedUser.getPassword());
// }
//
// @Test
// void save_method_should_throw_user_already_exist_exception_when_user_exist() {
// Mockito.when((clock.getZone())).thenReturn(NOW.getZone());
// Mockito.when((clock.instant())).thenReturn(NOW.toInstant());
// Mockito.when(userRepository.save(
// fakeUser.withPassword(
// encoder.encode(fakeUser.getPassword())
// )
// )).thenThrow(new UserAlreadyExistsException());
// //when
// //then
// Assertions.assertThrows(UserAlreadyExistsException.class,
// ()-> userService.save(fakeUser, "creatorId"));
// }
@Test
void save_method_should_return_saved_user_when_user_does_not_exist() {
Mockito.when((clock.getZone())).thenReturn(NOW.getZone());
Mockito.when((clock.instant())).thenReturn(NOW.toInstant());
Mockito.when(userRepository.save(Mockito.any(User.class))).thenReturn(fakeUser);

//when
User savedUser = userService.save(fakeUser, "creatorId");

//then
Assertions.assertNotNull(savedUser);
Assertions.assertEquals(fakeUser.getId(), savedUser.getId());
Assertions.assertEquals(fakeUser.getEmail(), savedUser.getEmail());
Assertions.assertEquals(fakeUser.getName(), savedUser.getName());
Assertions.assertEquals(fakeUser.getPassword(), savedUser.getPassword());
}

@Test
void save_method_should_throw_user_already_exist_exception_when_user_exist() {
Mockito.when((clock.getZone())).thenReturn(NOW.getZone());
Mockito.when((clock.instant())).thenReturn(NOW.toInstant());
Mockito.when(userRepository.save(Mockito.any(User.class)))
.thenThrow(new UserAlreadyExistsException());

//when
//then
Assertions.assertThrows(UserAlreadyExistsException.class,
()-> userService.save(fakeUser, "creatorId"));
}

@Test
void find_by_email_method_should_return_founded_user_when_user_exist() {
Expand Down

0 comments on commit 12032ef

Please sign in to comment.