### Question:
UserOperations { public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; } UserOperations(EmployeeRepo employeeRepo, BCryptPasswordEncoder encoder); DataResponse<BasicEmployee> getAllUsers(); GenericResponse userSignUp(EmployeeSignUpRequest request); GenericResponse deleteUser(Integer id); GenericResponse changePassword(UserChangePasswordRequest request); GenericResponse updateUser(EmployeeUpdateRequest request);  }### Answer:
@Test public void userSignUp() { Employee e = new Employee(); e.setPassword("password"); EmployeeSignUpRequest request = new EmployeeSignUpRequest(); request.setUsername("username"); request.setPassword("password"); request.setRole("ADMIN"); request.setFirstName("test"); request.setLastName("lastTest"); when(bCryptPasswordEncoder.encode(e.getPassword())).thenReturn("password"); when(employeeRepo.save(e)).thenReturn(e); GenericResponse response = operations.userSignUp(request); assertTrue(response.isSuccess() == true); assertTrue(response.getMessage().equalsIgnoreCase("User Sign Up Successful")); }
@Test public void userSignUpWithAnExistingUser() { Employee e = new Employee(); e.setUsername("username"); e.setPassword("password"); EmployeeSignUpRequest request = new EmployeeSignUpRequest(); request.setUsername("username"); request.setPassword("password"); request.setRole("ADMIN"); request.setFirstName("test"); request.setLastName("lastTest"); when(bCryptPasswordEncoder.encode(e.getPassword())).thenReturn(e.getPassword()); when(employeeRepo.findEmployeeByUsername(anyString())).thenReturn(e); GenericResponse response = operations.userSignUp(request); verify(employeeRepo, times(1)).findEmployeeByUsername(e.getUsername()); verify(employeeRepo, never()).save(e); assertFalse(response.isSuccess()); assertEquals(response.getMessage(), "User already exists"); assertEquals(response.getHttpStatus(), HttpStatus.BAD_REQUEST); }
@Test(expected = IdeaLabApiException.class) public void userSignUpFailValidation() { EmployeeSignUpRequest request = new EmployeeSignUpRequest(); request.setUsername("username"); request.setPassword(null); request.setRole("ADMIN"); request.setFirstName("test"); request.setLastName("lastTest"); operations.userSignUp(request); }
@Test public void userSignUpFail() { Employee e = new Employee(); e.setPassword("password"); EmployeeSignUpRequest request = new EmployeeSignUpRequest(); request.setUsername("username"); request.setPassword("password"); request.setRole("ADMIN"); request.setFirstName("test"); request.setLastName("lastTest"); when(bCryptPasswordEncoder.encode(e.getPassword())).thenReturn("password"); when(employeeRepo.save(any())).thenThrow(new RuntimeException()); GenericResponse response = operations.userSignUp(request); assertTrue(response.isSuccess() == false); assertTrue(response.getMessage().equalsIgnoreCase("User Sign Up Failed")); }