CurrentUserDtoProvider implements Provider<CurrentUserDto> { @Override public CurrentUserDto get() { HttpSession session = sessionProvider.get(); Long currentUserId = (Long) session.getAttribute(SecurityParameters.getUserSessionKey()); UserDto userDto = null; if (currentUserId != null) { userDto = User.createDto(userDao.get(currentUserId)); } boolean isLoggedIn = userDto != null; return new CurrentUserDto(isLoggedIn, userDto); } @Inject  CurrentUserDtoProvider(UserDao userDao,
            Provider<HttpSession> sessionProvider); @Override CurrentUserDto get();  }
@Test public void aValidSessionShouldReturnTheCurrentUser(User user) { given(httpSession.getAttribute(SecurityParameters.getUserSessionKey())).willReturn(A_USER_ID); given(userDao.get(A_USER_ID)).willReturn(user); UserDto userDto = User.createDto(user); CurrentUserDto currentUserDto = currentUserDtoProvider.get(); assertTrue(currentUserDto.isLoggedIn()); assertEquals(userDto, currentUserDto.getUser()); }
@Test public void anInvalidSessionShouldNotReturnTheCurrentUser() { given(httpSession.getAttribute(SecurityParameters.getUserSessionKey())).willReturn(null); given(userDao.get(anyLong())).willReturn(null); CurrentUserDto currentUserDto = currentUserDtoProvider.get(); assertFalse(currentUserDto.isLoggedIn()); assertNull(currentUserDto.getUser()); }