title: Spring: JUnit tests with session scope: Unsatisfied dependency expressed through field 'sessionTestBean'
body: <p>I tried to follow the guidelines at <a href="https://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/" rel="nofollow noreferrer">https://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/</a>
   or <a href="https://touk.pl/blog/2011/04/15/how-to-test-spring-session-scoped-beans/" rel="nofollow noreferrer">https://touk.pl/blog/2011/04/15/how-to-test-spring-session-scoped-beans/</a> .
  However, I cannot get my session beans autowired in a JUnit4 test.</p>
  
  <p>Maybe it is just some stupid error, however I cannot find it.</p>
  
  <p>I use spring-test 4.3.22.RELEASE (together with various other spring-libraries) and junit-4.12.jar</p>
  
  <p>Here is my trivial example (tested in eclipse Oxygen.3a Release (4.7.3a))</p>
  
  <p>TrivialSessionTest.java</p>
  
  <pre><code>package demo;
  
  import org.junit.Assert;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:spring/trivialApplicationContextForSessions.xml" })
  @ComponentScan("demo")
  public class TrivialSessionTests  {
  
  @Autowired
  protected SessionTestBean sessionTestBean;
  
  @Test
  public void testLogin() {
      Assert.assertEquals("Hello World", sessionTestBean.getSomething());
  }
  
  }
  </code></pre>
  
  <p>SessionTestBean.java</p>
  
  <pre><code>package demo;
  
  import org.springframework.context.annotation.Scope;
  import org.springframework.context.annotation.ScopedProxyMode;
  import org.springframework.stereotype.Component;
  
  @Component
  @Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
  public class SessionTestBean {
  
  public SessionTestBean() {}
  
  public String getSomething() {
      return "Hello World";
  }
  }
  </code></pre>
  
  <p>spring/trivialApplicationContextForSessions.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"&gt;
  
  &lt;bean id="sessionScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt;
      &lt;property name="scopes"&gt;
          &lt;map&gt;
              &lt;entry key="session"&gt;
                  &lt;bean class="org.springframework.context.support.SimpleThreadScope" /&gt;
              &lt;/entry&gt;
              &lt;entry key="request"&gt;
                  &lt;bean class="org.springframework.web.context.request.RequestScope" /&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Running the test results in</p>
  
  <pre><code>[main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener     [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@73e132e0] to prepare test instance [demo.TrivialSessionTests@3773862a]
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo.TrivialSessionTests': Unsatisfied dependency expressed through field 'sessionTestBean'; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'demo.SessionTestBean' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>