title: JUnit testing MyBatis-Spring project
body: <p>An <code>IllegalStateException</code> gets thrown on attempting to JUnit test a MyBatis-Spring project.</p>
  
  <p>The problem appears to be with <strong>Autowiring</strong> MyBatis Mapper Beans (pardon my jargon since I'm new to the whole MyBatis-Spring-JUnit setup). I inherited the setup from somebody else. Let me know if any other information is relevent to get help.</p>
  
  <p>The setup described below works when I expose the whole thing as a web-service. But, it fails when I try to JUnit test it. How can I Autowire the mapper <strong>while testing</strong>?</p>
  
  <h1>Setup</h1>
  
  <h3>Under main/java</h3>
  
  <ul>
  <li>persistence folder: ProductMapper.java</li>
  <li>service folder: ProductService.java</li>
  <li>ws folder: BrowserService.java</li>
  <li>ws/impl folder: BrowserServiceImpl.java</li>
  </ul>
  
  <h3>Under main/resources</h3>
  
  <ul>
  <li>persistence folder: ProductMapper.xml</li>
  </ul>
  
  <h3>BrowserService.java</h3>
  
  <pre><code>class BrowserServiceImpl {
  
      private ProductService productService;
  
      // setters and getters
      // Methods
  }
  </code></pre>
  
  <h3>ProductService.java</h3>
  
  <pre><code>class ProductService {
  
      @Autowired
      private ProductMapper productMapper;
  
      // Methods
  }
  </code></pre>
  
  <h3>Context.xml</h3>
  
  <pre><code>&lt;beans&gt;
      &lt;bean id="browserSvc" class="com.comp.team.proj.ws.impl.BrowserServiceImpl"&gt;
          &lt;property name="productService" ref="productService" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- MyBatis Config --&gt;
      &lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt;
          &lt;property name="basePackage" value="com.comp.team.proj.persistence" /&gt;
          &lt;property name="annotationClass" value="org.springframework.stereotype.Repository"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.OracleDriver"/&gt;
          &lt;property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.com:xxxx:xxx"/&gt;
          &lt;property name="username" value="myusername"/&gt;
          &lt;property name="password" value="mypassword"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Other beans --&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>If you are curious what the Exception looks like:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  ...
  Caused by: org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: private com.comp.team.proj.persistence.ProductMapper 
      com.comp.team.proj.service.ProductService.productMapper;
  
  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [com.comp.team.proj.persistence.ProductMapper]
      found for dependency:
         expected at least 1 bean which qualifies as autowire candidate for this 
         dependency.
  ...
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>See the dataSource <em>Bean</em> in the updated <code>Context.xml</code> above. Does the configuration look correct?</p>
  
  <p>Looks like one problem is with the <strong>dataSource</strong> setup. I make this assumption because the JUnit test runs, but throws an exception. So, it's not the JUnit setup most likely. If the dataSource setup is correct, MyBatis will successfully instantiate the <strong>Mapper</strong> Bean and the Autowiring will succeed.</p>
  
  <h2>Test File</h2>
  
  <pre><code>@ContextConfiguration
  @RunWith(SpringJUnit4ClassRunner.class)
  public class ProductCountTest {
  
      @Autowired
      protected BrowserService browserSvc;
  
      @Test
      public void testGetProductCount() {
          long count = browserSvc.getProductCount();
  
          assertTrue(count &gt; 0);
      }
  
  }
  </code></pre>
  
  <p>It finds the <code>Context.xml</code> file without any problem as I placed it in the right directory.</p>