How to mock user session in Vaadin7 ?

I’m trying to mock user session with Mockito but it is failing.

For user data I’ve created UserSession class which consist of id, userName etc.
It is stored in Vaadin session this way:

UserSession user = UserSession.builder().id(personId).userName(username).name(name).build(); VaadinSession.getCurrent().setAttribute(USER_SESSION_ATTRIBUTE_NAME, user); Then, I’d like to mock user session in test like this:

[code]
Mockito.mock(Ui.class);
WrappedSession wrappedSession = Mockito.mock(WrappedSession.class);
UserSession userSession = UserSession.builder()
.id(1)
.name(“name”)
.userName(“name”)
.build();
wrappedSession.setAttribute(USER_SESSION_ATTRIBUTE_NAME, userSession);

VaadinSession mockedVaadinSession = Mockito.mock(VaadinSession.class);
Mockito.when(mockedVaadinSession.getState()).thenReturn(VaadinSession.State.OPEN);
Mockito.when(mockedVaadinSession.getSession()).thenReturn(wrappedSession);
Mockito.when(mockedVaadinSession.getService()).thenReturn(Mockito.mock(VaadinService.class));
CurrentInstance.set(VaadinSession.class, mockedVaadinSession);

assertTrue(VaadinSession.getCurrent().getAttribute(USER_SESSION_ATTRIBUTE_NAME) != null);
[/code]Assertion is failing. Question is what is missing?

Hi,

Is it because you’re setting the USER_SESSION_ATTRIBUTE_NAME to the WrappedSession and not the VaadinSession?

-Olli