### Question:
UserServiceImpl implements UserService { @Override public UserDetails getUserByEmailId(final String emailId) throws PacManException { User user; if (emailId != null) { user = userRepository.findByEmailIgnoreCase(emailId); return fetchUserDetails(user, emailId); } else { throw new PacManException("Email Id cannot be empty"); } }  @Override List<User> getAllLoginUsers(); @Override UserDetails getUserByEmailId(final String emailId);  }### Answer:
@Test public void getUserByEmailIdTest() throws PacManException { User user = getUserDetails(); List<String[]> userRoles = getUserRoles(); UserPreferences userPreferences = getUserPreferences(); when(userRepository.findByEmailIgnoreCase(anyString())).thenReturn(user); when(userRolesMappingRepository.findAllUserRoleDetailsByUserIdIgnoreCase(user.getUserId())).thenReturn(userRoles); when(userPreferencesService.getUserPreferencesByNtId(user.getUserId())).thenReturn(userPreferences); assertThat(userService.getUserByEmailId(anyString()).getEmail(), is("email123")); }
@Test public void getUserByEmailIdEmailIdNotFoundTest() throws PacManException { User user = getUserDetails(); when(userRepository.findByEmailIgnoreCase(anyString())).thenReturn(user); assertThatThrownBy(() -> userService.getUserByEmailId(null)).isInstanceOf(PacManException.class); }
@Test public void getUserByEmailIdUserNotFoundTest() throws PacManException { when(userRepository.findByEmailIgnoreCase(anyString())).thenReturn(null); assertThat(userService.getUserByEmailId(anyString()).getEmail(), is("")); }
@Test public void getUserByEmailIdEmailIdFoundTest() throws PacManException { when(userRepository.findByEmailIgnoreCase(anyString())).thenReturn(null); assertThat(userService.getUserByEmailId("emailId").getEmail(), is("emailId")); }