title: Not able to test SessionRegistry in Spring Boot
body: <p>I am using session registry to maintain a single session.</p>
  <p>The code is like :</p>
  <pre><code>@Component
  public class AppLogout implements LogoutSuccessHandler{
  
  @Autowired
  private SessionRegistry sessionRegistry
  
  ...
  
  List&lt;SessionInformation&gt; sessions = sessionRegistry.getAllSessions(auth.getPrincipal(), false)
  </code></pre>
  <p>Now whenever I am trying to write testcase for this am getting either <code>NullPointerException</code> on the 2nd line for <code>sessionRegistry</code> or not a mock exception.</p>
  <p>Things Tried :</p>
  <ol>
  <li>Can't do injectMocks on sessionRegistry since it's an interface</li>
  </ol>
  <pre><code>@Mock
  SessionRegistry sg;
  
  @InjectMocks
  SessionRegistryImpl sgImpl 
  </code></pre>
  <ol start="2">
  <li>Using <code>@Mock</code> leads to <code>NullPointerException</code></li>
  </ol>
  <pre><code>@Mock
  SessionRegistry sg
  
  Mockito.doReturn(list).when(sg).... (NullPointer on sg)
  </code></pre>
  <ol start="3">
  <li>Removing the mock completely but still getting nullPointer on <code>sessionRegistry.getAllSessions(...)</code></li>
  </ol>
  <p>Actual Class :</p>
  <pre><code>@Component
  public class AppLogoutHandler implements LogoutSuccessHandler{
      
      @Autowired
      SessionRegistry sessionRegistry;
      
      
      @Override
      public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
              throws IOException, ServletException {
          
          if(authentication != null &amp;&amp; authentication.getDetails() != null) {
              removeAuthSession(authentication, sessionRegistry);
              request.getSession().invalidate();
          }
          
          
      }
  
  
      private void removeAuthSession(Authentication authentication, SessionRegistry sessionRegistry2) {
          
          List&lt;SessionInformation&gt; session= sessionRegistry.getAllSessions(authentication.getPrincipal(), false); //sessionRegistry here is giving issue
          
          if(!session.isEmpty()) {
              ....
          }
      }
  
  }
  </code></pre>
  <p>Test Class (This is the latest version, Have tried the things mentioned in point 1,2 also)</p>
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  public class AppLogoutSuccessHandlerTest {
  
      @MockBean
      SessionRegistry registry;
      
      @InjectMocks
      AppLogoutHandler appLogoutHandler;
      
      @org.junit.Before
      public void setup() {
      MockitoAnnotations.initMocks(this); 
      }
      
      @Test
      public void onLogoutSuccessTest() {
          //Mocked ServletRequest, Response here
          Object object = new Object();
          List&lt;SessionInformation&gt; informations = new ArrayList&lt;SessionInformation&gt;();
          //informations.add(sessionInformation); SessionInformation is created above and passed
          
          Mockito.doReturn(informations).when(registry).getAllSessions(object, false); //If i include this line it gives not a mock on when and if i doesn't then it is giving nullPointer on actual class
          
      }
  }
  </code></pre>
  <p>Updated Test Case (This is giving null pointer on execution for line -&gt; sessionRegistry.getAllSessions(authentication.getPrincipal(), false); )</p>
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class AppLogoutSuccessHandlerTest {
  
      @Mock
      SessionRegistry registry;
      
      @InjectMocks
      AppLogoutHandler appLogoutHandler;
      
      /*
       * @org.junit.Before public void setup() { MockitoAnnotations.initMocks(this); }
       */
      
      @Test
      public void onLogoutSuccessTest() {
          //Mocked ServletRequest, Response here
          Object object = new Object();
          List&lt;SessionInformation&gt; informations = new ArrayList&lt;SessionInformation&gt;();
          //informations.add(sessionInformation); SessionInformation is created above and passed
          
          Mockito.doReturn(informations).when(registry).getAllSessions(object, false); //If i include this line it gives not a mock on when and if i doesn't then it is giving nullPointer on actual class
          
      }
  }
  </code></pre>
  <p>I have tried the annotation <code>@SpringBootTest</code> and without it but no luck.</p>
  <p>Can you suggest what is going wrong with the above ways.</p>