An IllegalStateException
gets thrown on attempting to JUnit test a MyBatis-Spring project.
The problem appears to be with Autowiring 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.
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 while testing?
Setup
Under main/java
- persistence folder: ProductMapper.java
- service folder: ProductService.java
- ws folder: BrowserService.java
- ws/impl folder: BrowserServiceImpl.java
Under main/resources
- persistence folder: ProductMapper.xml
BrowserService.java
class BrowserServiceImpl {
private ProductService productService;
// setters and getters
// Methods
}
ProductService.java
class ProductService {
@Autowired
private ProductMapper productMapper;
// Methods
}
Context.xml
<beans>
<bean id="browserSvc" class="com.comp.team.proj.ws.impl.BrowserServiceImpl">
<property name="productService" ref="productService" />
</bean>
<!-- MyBatis Config -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.comp.team.proj.persistence" />
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.com:xxxx:xxx"/>
<property name="username" value="myusername"/>
<property name="password" value="mypassword"/>
</bean>
<!-- Other beans -->
</beans>
If you are curious what the Exception looks like:
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.
...
EDIT
See the dataSource Bean in the updated Context.xml
above. Does the configuration look correct?
Looks like one problem is with the dataSource 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 Mapper Bean and the Autowiring will succeed.
Test File
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductCountTest {
@Autowired
protected BrowserService browserSvc;
@Test
public void testGetProductCount() {
long count = browserSvc.getProductCount();
assertTrue(count > 0);
}
}
It finds the Context.xml
file without any problem as I placed it in the right directory.