### Question:
UserService extends
    PersonService<E, D> { @Transactional(readOnly = true) public E getUserBySession() { final Object principal = SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); if (!(principal instanceof User)) { return null; } @SuppressWarnings("unchecked") E loggedInUser = (E) principal; Integer id = loggedInUser.getId(); return dao.findById(id); } @SuppressWarnings("unchecked")  UserService(); protected  UserService(Class<E> entityClass); @Override @Autowired @Qualifier("userDao") void setDao(D dao); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#accountName, 'READ')") @Transactional(readOnly = true) E findByAccountName(String accountName); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#email, 'READ')") @Transactional(readOnly = true) E findByEmail(String email); E registerUser(E user, HttpServletRequest request); @SuppressWarnings("unchecked") void activateUser(String tokenValue); E persistNewUser(E user, boolean encryptPassword); void updatePassword(E user, String rawPassword); @Transactional(readOnly = true) E getUserBySession(); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) Set<UserGroup> getGroupsOfUser(Integer userId); PasswordEncoder getPasswordEncoder(); void setPasswordEncoder(PasswordEncoder passwordEncoder); Role getDefaultUserRole(); void setDefaultUserRole(Role defaultUserRole);  }### Answer:
@Test public void getUserBySession_shouldReturnUserFromSession() throws NoSuchFieldException, IllegalAccessException { User incompleteSecurityContextUser = new User(); Integer userId = 42; IdHelper.setIdOnPersistentObject(incompleteSecurityContextUser, userId); String accountName = "someUser"; String firstName = "John"; User completeUserFromDatabase = new User(); IdHelper.setIdOnPersistentObject(completeUserFromDatabase, userId); completeUserFromDatabase.setAccountName(accountName); completeUserFromDatabase.setFirstName(firstName); Authentication authentication = new UsernamePasswordAuthenticationToken(incompleteSecurityContextUser, "somePw"); SecurityContextHolder.getContext().setAuthentication(authentication); when(dao.findById(userId)).thenReturn(completeUserFromDatabase); User fullUserBySession = crudService.getUserBySession(); assertEquals(userId, fullUserBySession.getId()); assertEquals(accountName, fullUserBySession.getAccountName()); assertEquals(firstName, fullUserBySession.getFirstName()); verify(dao, times(1)).findById(userId); verifyNoMoreInteractions(dao); }