title: Testing a Spring MVC Handler interceptor with a session scoped bean dependency
body: <p>I have a problem I'm not able to solve. I have searched on the internet and on Stackoverflow but could not find how to solve the problem.</p>
  
  <p>I want to test a Spring MVC Handler interceptor. This interceptor has a "session" scope bean as a dependency. </p>
  
  <p>I tried to reduce the code as much as possible. Here is the code:</p>
  
  <p>The src part :</p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  @ComponentScan(basePackages = { "..." })
  public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
  
      @Override
      public void addInterceptors(InterceptorRegistry interceptorRegistry) {
          interceptorRegistry.addInterceptor(initializeUserLanguageHandler());
      }
  
      @Bean
      public InitializeUserLanguageHandler initializeUserLanguageHandler() {
          return new InitializeUserLanguageHandler();
      }
  
      @Bean
      @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
      public SessionBean sessionBean() {
          return new SessionBean();
      }
  }
  
  
  @Component
  public class InitializeUserLanguageHandler extends AbstractHandlerInterceptor {
  
      @Autowired
      private SessionBean sessionBean;
  
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          if (sessionBean.getLanguage() == null) {
              sessionBean.setLanguage(getUserLanguage());
          }
  
          return true;
      }
  }
  </code></pre>
  
  <p>The test part:</p>
  
  <pre><code>@WebAppConfiguration
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = SpringMvcConfiguration.class)
  public class BaseSpringMvcIntegrationTest {
  
      @Resource
      protected WebApplicationContext webApplicationContext;
  
      protected MockMvc mockMvc;
  
      @Before
      public void setUp() {
          mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      }
  }
  
  public class InitializeUserLanguageHandlerTest extends BaseSpringMvcIntegrationTest {
  
      @Autowired
      private SessionBean sessionBean;
  
      @Autowired
      private MockHttpSession mockHttpSession;
  
      @Test
      public void testLanguageIsInitializedOnlyOnce() throws Exception {
          MockHttpSession mocksession = new MockHttpSession();
  
          // It is null, this is because the interceptor has not been called yet
          assertEquals(null, sessionBean.getLanguage());
  
          // This line will call the interceptor and set language to "nl"
          mockMvc.perform(get("/").session(mocksession).principal(getUser("nl")));
  
          // It is null, but I expect it to be "nl" 
          assertEquals(null, sessionBean.getLanguage()); 
  
          // Let's try again     
          mockMvc.perform(get("/").session(mocksession).principal(getUser("fr")));
  
          // It is null, but I expect it to be "nl" 
          assertEquals(null, sessionBean.getLanguage());
      }
  }
  </code></pre>
  
  <p>You can see in the test class "InitializeUserLanguageHandlerTest" that I have some assertions.</p>
  
  <p>The first time I call:</p>
  
  <pre><code>mockMvc.perform(get("/").session(mocksession).principal(getUser()));
  </code></pre>
  
  <p>The code in the interceptor is executed and language is set to "nl". Therefore, in my test, I would have expected that sessionBean.getLanguage() would return me "nl", but it is not. I don't understand why.</p>
  
  <p>So I'm calling the perform again, the interceptor code is executed again, and calling sessionBean.getLanguage() returns "nl". </p>
  
  <p>It seems I've two SessionBean instances, one in my test and the other in the source. But when I look at the SessionBean variable in Eclipse in Debug mode, they have the same ID. </p>
  
  <p>If I change the "session" scope to "application" scope, it is working properly.</p>
  
  <p>Can somebody help me ?</p>
  
  <p>Thank you.</p>