Reputation: 1646
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?
class BrowserServiceImpl {
private ProductService productService;
// setters and getters
// Methods
}
class ProductService {
@Autowired
private ProductMapper productMapper;
// Methods
}
<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.
...
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.
@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.
Upvotes: 1
Views: 15430
Reputation: 1646
Correct Data-Source config here.
I discovered that there was another way to do the mapping bean:
<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.comp.team.proj.persistence.ProductMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
I think the IllegalStateException
was thrown because:
The data-source was incorrectly configured, which led to the mapping to fail resulting in the Mapper bean not being created (NoSuchBeanDefinitionException
).
Upvotes: 0
Reputation: 525
Please provide us the code of unit test.
I suppose that you are missing spring configuration
@ContextConfiguration(locations = {
"classpath:/com/comp/team/proj/context.xml" // give the correct path
})
@RunWith(SpringJUnit4ClassRunner.class)
public class BrowserServiceTest {
@Autowired
BrowserServiceImpl browserService;
@Test
public void shouldTestSmth(){
}
Remember that during testing Spring uses spring-test lib.
EDIT: I suppose that the most important part is still not shown. Do you have bean definition of ProductMapper in your context.xml? I suppose that it's missing.
I can also see that you're mixing annotations and bean definitions. Maybe you need bean definition?
Upvotes: 1