3

I am using Spring framework 2.4.4 and Intellij Ultimate 2020

I would like to test this method tryGetUser on which I am passing HttpSession, but I can't resolve the problem, because when I am mocking it and setAttribute(); at the //Arrange part of the method it always ends with currentUserUsername to be null.

public User tryGetUser(HttpSession session) {
    String currentUserUsername = (String) session.getAttribute("currentUserUsername");
***// Here this local variable currentUsername is always null;***
    if (currentUserUsername == null) {
        throw new AuthorizationException("No logged in user.");
    }

    try {
        return userService.getByUsername(currentUserUsername);
    } catch (EntityNotFoundException e) {
        throw new AuthorizationException("No logged in user.");
    }
}

Here you can see how I am trying to Mock it, but actually, the session is remaining empty or I don't know, but when the service method starts to execute the attribute is not there.

@ExtendWith(MockitoExtension.class)
public class LoginServiceMvcTests {

    @Mock
    UserRepository mockUserRepository;

    @Mock
    UserService mockUserService;

    @InjectMocks
    MockHttpSession mockHttpSession;

    @InjectMocks
    LoginServiceMvc userService;

    private User junkie;
    private User organizer;

    @BeforeEach
    public void setup() {
        junkie = Helpers.createJunkie();
        organizer = Helpers.createOrganizer();
    }

    @Test
    public void tryGetUser_Should_GetUserFromSession_When_UserIsLogged() {

        // Arrange
        mockHttpSession.setAttribute("luboslav", junkie);
        Mockito.when(mockUserService.getByUsername("luboslav"))
                .thenReturn(junkie);

        // Act
        userService.tryGetUser(mockHttpSession);

        // Assert
        Mockito.verify(mockUserService, Mockito.times(1))
                .getByUsername("luboslav");
    }
}
HoRn
  • 1,458
  • 5
  • 20
  • 25
oneofakind
  • 33
  • 1
  • 7

2 Answers2

3

From Jon skeet answer Mockito doesn't work that way, so you need to actually mock the call instead of setting attribute

Mockito.when(mockHttpSession. getAttribute("currentUserUsername"))
            .thenReturn("name");

And also HttpSession should be annotated with @Mock not @InjectMocks

@Mock
HttpSession httpSession;
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

Use MockHttpSession, which is an actual real object (not just a Mockito interface mock) that is intended for exactly this type of testing. It's basically an empty/blank container, so you don't have to mock out all the standard behavior, just fill it with whatever attributes you want.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152