title: wicket 9: unit testing + mockito + httpSession
body: <p><strong>A Situation</strong></p>
  <p>I'm using wicket 9 for a web gui development. And now I'm trying to do some junit testing by mocking (mockito) methods that are not essential. One of those methods is</p>
  <pre><code>httpSession.getAttribute(&quot;loginName&quot;);
  </code></pre>
  <p>which gets called like this:</p>
  <pre><code>public class MyPanel {
           ...
  
       SecureWebSession session = (SecureWebSession) getSession(); &lt;--returns a SecuredWebSession
       Label username = new Label(&quot;loginName&quot;, session.getLoginName()); &lt;-- NullPointerException
          ...
  }
  </code></pre>
  <p>Obviously, something is not right with how I'm mocking things. I expect it to return test, but all I get at all times is a NullPointerException.</p>
  <p>My question is how can I bypass session.getLoginName() so, that the call would return
  a login name test?</p>
  <p>How to mock httpSession while testing wicket application?</p>
  <p><strong>Details</strong></p>
  <p>I have a securedWebSession class</p>
  <pre><code>package com.my.app.application;
  
  import com.my.app.service.signOnService;
  import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
  import org.apache.wicket.authroles.authorization.strategies.role.Roles;
  import org.apache.wicket.injection.Injector;
  import org.apache.wicket.request.Request;
  import org.apache.wicket.spring.injection.annot.SpringBean;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.context.SecurityContextHolder;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import java.io.UnsupportedEncodingException;
  
  public class SecureWebSession extends AuthenticatedWebSession {
  
      @SpringBean(name = &quot;authenticationManager&quot;)
      private AuthenticationManager authenticationManager;
  
      @SpringBean(name = &quot;signOnService&quot;)
      private SignOnService signOnService;
  
      private final HttpSession httpSession;
  
      private final Request request;
  
      public SecureWebSession(Request request) {
          super(request);
          this.request = request;
          //******************THIS I CANNOT BYPASS***************************************START
          this.httpSession = ((HttpServletRequest) request.getContainerRequest())
                  .getSession(false);
          //******************THIS I CANNOT BYPASS******ALLWAYS RETURNS NULL*************END
          
          Injector.get().inject(this);
      }
  
      @Override
      public boolean authenticate(String username, String password) {
          return true;
      }
  
      private boolean hasSignedIn(Authentication authentication) {
          return authentication.isAuthenticated();
      }
  
      @Override
      public Roles getRoles() {
          Roles roles = new Roles();
          roles.add(&quot;SUPER_ADMIN&quot;);
          return roles;
      }
  
      public String getLoginName() {
       ********RETURNS NULL.getAttribute(&quot;loginName&quot;)******
          return (String) httpSession.getAttribute(&quot;loginName&quot;);
      }
  }
  </code></pre>
  <p>I have a test setup as following</p>
  <pre><code>@ExtendWith(SpringExtension.class)
  @SpringBootTest(classes = {
    MyWicketApplication.class
  })
  public class ApplicationTest {
  
    private WicketTester tester;
  
    private final WebApplication webApplication;
  
    @Autowired
    public SecuredWicketGuiApplicationTest(WebApplication application) {
      this.webApplication = application;
    }
  
    @BeforeEach
    public void setUp() {
  
      //**** ** * WORKS ** ** ** **
      // when authenticated is given
      Authentication authentication = Mockito.mock(Authentication.class);
      SecurityContext securityContext = Mockito.mock(SecurityContext.class);
      Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
      Mockito.when(authentication.isAuthenticated()).thenReturn(true);
      SecurityContextHolder.setContext(securityContext); 
      //** ** ** ** ** ** ** ** ** *
  
      // and secured session is provided
      SecureWebSession secureWebSession = Mockito.mock(SecureWebSession.class);
  
      //**** ** * WORKS ** ** ** **
      // and a superuser role is given
      Roles roles = new Roles();
      roles.add(&quot;SUPER_ADMIN&quot;);
      Mockito.when(secureWebSession.getRoles()).thenReturn(roles); 
      //** ** ** ** ** ** ** ** **
  
      // and session has loginName attribute
      HttpSession session = Mockito.mock(HttpSession.class);
      Mockito.when(session.getAttribute(&quot;loginName&quot;)).thenReturn(&quot;test&quot;);
  
      HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
      Mockito.when(request.getSession()).thenReturn(session);
  
      tester = new WicketTester(webApplication);
  
      MockHttpSession mockHttpSession = new MockHttpSession(null);
      mockHttpSession.setAttribute(&quot;loginName&quot;, &quot;test&quot;);
  
      // and a requested initiated for a given session
      MockHttpServletRequest mockedRequest = new MockHttpServletRequest(
        tester.getApplication(), mockHttpSession, null) {
        @Override
        public HttpSession getSession(boolean createNew) {
          return mockHttpSession;
        }
  
        @Override
        public HttpSession getSession() {
          return mockHttpSession;
        }
      };
  
      // and a session object is created with a loginName attribute test
      tester.getRequest().getSession()
        .setAttribute(&quot;loginName&quot;, &quot;test&quot;);
  
      tester.setRequest(mockedRequest);
    }
  
    @Test
    public void HomePage_Renders_Successfully() {
      // then start and render the homePage page
      tester.startPage(HomePage.class);
      // assert rendered HomePage component
      tester.assertRenderedPage(HomePage.class);
    }
  }
  </code></pre>
  <p>when I try to execute this test I get stuck on</p>
  <pre><code>public class MyPanel {
       ...
  
   SecureWebSession session = (SecureWebSession) getSession(); &lt;--returns a SecuredWebSession
   Label username = new Label(&quot;loginName&quot;, session.getLoginName()); &lt;-- NullPointerException
      ...
  }
  </code></pre>
  <p>Where method getLoginName is defined in securedWebSession</p>
  <pre><code>public String getLoginName() {
   ********RETURNS NULL.getAttribute(&quot;loginName&quot;)******
      return (String) httpSession.getAttribute(&quot;loginName&quot;);
  }
  </code></pre>