### Question:
UserRolesServiceImpl implements UserRolesService { @Override public String updateUserRole(final UpdateRoleDetailsRequest roleDetailsRequest, final String userId) throws PacManException { boolean isRoleNameExits = userRolesRepository.existsById(roleDetailsRequest.getRoleId()); if (isRoleNameExits) { try { Date currentDate = new Date(); UserRoles userRole = userRolesRepository.findById(roleDetailsRequest.getRoleId()).get(); userRole.setModifiedDate(currentDate); userRole.setOwner(userId); userRole.setRoleDesc(roleDetailsRequest.getDescription()); userRole.setRoleName(roleDetailsRequest.getRoleName()); userRole.setWritePermission(roleDetailsRequest.getWritePermission()); userRolesRepository.save(userRole); return USER_ROLE_UPDATION_SUCCESS; } catch (Exception exception) { log.error(UNEXPECTED_ERROR_OCCURRED, exception); throw new PacManException(UNEXPECTED_ERROR_OCCURRED); } } else { throw new PacManException(USER_ROLE_NOT_EXITS); } }  @Override Page<UserRolesResponse> getAllUserRoles(String searchTerm, int page, int size); @Override String createUserRole(final CreateRoleDetailsRequest roleDetailsRequest, final String userId); @Override String updateUserRole(final UpdateRoleDetailsRequest roleDetailsRequest, final String userId); @Override UserRoles getUserRoleById(final String roleId);  }### Answer:
@Test public void updateUserRoleTest() throws PacManException { UpdateRoleDetailsRequest roleDetailsRequest = getUpdateRoleDetailsRequest(); Optional<UserRoles> userRolesDetail = getUserRoleDetails(); when(userRolesRepository.findById(anyString())).thenReturn(userRolesDetail); when(userRolesRepository.existsById(anyString())).thenReturn(true); when(userRolesRepository.save(userRolesDetail.get())).thenReturn(userRolesDetail.get()); assertThat(userRolesService.updateUserRole(roleDetailsRequest, anyString()), is(USER_ROLE_UPDATION_SUCCESS)); }
@Test public void updateUserRoleUserNotFoundTest() throws PacManException { UpdateRoleDetailsRequest roleDetailsRequest = getUpdateRoleDetailsRequest(); Optional<UserRoles> userRolesDetail = getUserRoleDetails(); when(userRolesRepository.findById(anyString())).thenReturn(userRolesDetail); when(userRolesRepository.existsById(anyString())).thenReturn(false); when(userRolesRepository.save(userRolesDetail.get())).thenReturn(userRolesDetail.get()); assertThatThrownBy(() -> userRolesService.updateUserRole(roleDetailsRequest, anyString())).isInstanceOf(PacManException.class); }
@SuppressWarnings("unchecked") @Test public void updateUserRoleUserNotFoundExceptionTest() throws PacManException { UpdateRoleDetailsRequest roleDetailsRequest = getUpdateRoleDetailsRequest(); Optional<UserRoles> userRolesDetail = getUserRoleDetails(); when(userRolesRepository.findById(anyString())).thenThrow(Exception.class); when(userRolesRepository.existsById(anyString())).thenReturn(true); when(userRolesRepository.save(userRolesDetail.get())).thenReturn(userRolesDetail.get()); assertThatThrownBy(() -> userRolesService.updateUserRole(roleDetailsRequest, anyString())).isInstanceOf(PacManException.class); }