title: IllegalStateException: No Scope registered for scope 'session' on unit test
body: <p>I have a modified version of the <a href="http://java.lang.IllegalStateException:%20No%20Scope%20registered%20for%20scope%20&#39;session&#39;" rel="noreferrer">mkyong MVC tutorial.</a> </p>
  
  <p>I've added a business layer class <code>Counter</code>.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Counter {
  
      private int i;
  
  
      public int count()
      {
          return (this.i++);
      }
  
      //getters and setters and constructors
  }
  </code></pre>
  
  <p>In mvc-dispatcher-servlet.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="counter" class="com.mkyong.common.Counter" scope="session"&gt;
      &lt;property name="i" value="0"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>This works fine.</p>
  
  <p>I now want to create a unit test for this class</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration()
  
  public class TestCounter {
  
      @Configuration
      static class TestConfig
      {
          @Bean
          public Counter c()
          {
              return new Counter();
          }
      }
  
      @Autowired
      private Counter c;
  
      @Test
      public void count_from1_returns2()
      {
          c.setI(1);
          assertEquals(2, c.count());
  
      }
  
  }
  </code></pre>
  
  <p>If I run it like this, I'll get </p>
  
  <pre><code>SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@655bf451] to prepare test instance [com.mkyong.common.TestCounter@780525d3]
  org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/mkyong/common/TestCounter-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/mkyong/common/TestCounter-context.xml] cannot be opened because it does not exist
  </code></pre>
  
  <p>So we need to specify where our context is:</p>
  
  <pre class="lang-java prettyprint-override"><code>@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
  </code></pre>
  
  <p>Now if I run this I get:</p>
  
  <pre><code>SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5a2611a6] to prepare test instance [com.mkyong.common.TestCounter@7950d786]
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mkyong.common.TestCounter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mkyong.common.Counter com.mkyong.common.TestCounter.c; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'session'
  </code></pre>
  
  <p>Why is this happening, and how do I resolve it?</p>