@Test public void userDeleteFail() { Employee e = new Employee(); e.setId(1); when(employeeRepo.findEmployeeById(e.getId())).thenReturn(e); doThrow(new RuntimeException()).when(employeeRepo).deleteById(1); GenericResponse response = operations.deleteUser(1); assertTrue(response.isSuccess() == false); assertTrue(response.getMessage().equalsIgnoreCase("Employee Could Not Be Deleted")); }
public GenericResponse deleteUser(Integer id) { GenericResponse response = new GenericResponse(); Employee e = employeeRepo.findEmployeeById(id); if(e != null) { try { employeeRepo.deleteById(id); response.setSuccess(true); response.setMessage("Employee Deleted Successfully"); response.setHttpStatus(HttpStatus.ACCEPTED); } catch (Exception ex) { ex.printStackTrace(); response.setSuccess(false); response.setMessage("Employee Could Not Be Deleted"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } } else { response.setSuccess(false); response.setMessage("Employee ID is not valid"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } return response; }
UserOperations { public GenericResponse deleteUser(Integer id) { GenericResponse response = new GenericResponse(); Employee e = employeeRepo.findEmployeeById(id); if(e != null) { try { employeeRepo.deleteById(id); response.setSuccess(true); response.setMessage("Employee Deleted Successfully"); response.setHttpStatus(HttpStatus.ACCEPTED); } catch (Exception ex) { ex.printStackTrace(); response.setSuccess(false); response.setMessage("Employee Could Not Be Deleted"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } } else { response.setSuccess(false); response.setMessage("Employee ID is not valid"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } return response; } }
UserOperations { public GenericResponse deleteUser(Integer id) { GenericResponse response = new GenericResponse(); Employee e = employeeRepo.findEmployeeById(id); if(e != null) { try { employeeRepo.deleteById(id); response.setSuccess(true); response.setMessage("Employee Deleted Successfully"); response.setHttpStatus(HttpStatus.ACCEPTED); } catch (Exception ex) { ex.printStackTrace(); response.setSuccess(false); response.setMessage("Employee Could Not Be Deleted"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } } else { response.setSuccess(false); response.setMessage("Employee ID is not valid"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } return response; } UserOperations(EmployeeRepo employeeRepo, BCryptPasswordEncoder encoder); }
UserOperations { public GenericResponse deleteUser(Integer id) { GenericResponse response = new GenericResponse(); Employee e = employeeRepo.findEmployeeById(id); if(e != null) { try { employeeRepo.deleteById(id); response.setSuccess(true); response.setMessage("Employee Deleted Successfully"); response.setHttpStatus(HttpStatus.ACCEPTED); } catch (Exception ex) { ex.printStackTrace(); response.setSuccess(false); response.setMessage("Employee Could Not Be Deleted"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } } else { response.setSuccess(false); response.setMessage("Employee ID is not valid"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } 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); }
UserOperations { public GenericResponse deleteUser(Integer id) { GenericResponse response = new GenericResponse(); Employee e = employeeRepo.findEmployeeById(id); if(e != null) { try { employeeRepo.deleteById(id); response.setSuccess(true); response.setMessage("Employee Deleted Successfully"); response.setHttpStatus(HttpStatus.ACCEPTED); } catch (Exception ex) { ex.printStackTrace(); response.setSuccess(false); response.setMessage("Employee Could Not Be Deleted"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } } else { response.setSuccess(false); response.setMessage("Employee ID is not valid"); response.setHttpStatus(HttpStatus.BAD_REQUEST); } 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);  }