Spring 3 unit testing
I'm trying to write a unit test for my DAO class, but the thing is I get this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.yeah.server.dao.UserDAOTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.yeah.server.dao.UserDAO com.yeah.server.dao.UserDAOTest.userDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.yeah.server.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Here's my applicationContext file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<context:annotation-config />
<context:component-scan base-package="com.yeah.server.*" />
<aop:aspectj-autoproxy />
<!--Mysql database connection info-->
<bean name="dataSource" id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.1.4:3306/YeaH"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/yeah/shared/model/User.hbm.xml</value>
<value>com/yeah/shared/model/Comment.hbm.xml</value>
<value>com/yeah/shared/model/Album.hbm.xml</value>
<value>com/yeah/shared/model/Picture.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Gilead -->
<bean id="proxySerializer" class="net.sf.gilead.core.serialization.GwtProxySerialization" />
<bean id="proxyStore" class="net.sf.gilead.core.store.stateless.StatelessProxyStore">
<property name="proxySerializer" ref="proxySerializer" />
</bean>
<bean id="persistenceUtil" class="net.sf.gilead.core.hibernate.HibernateUtil">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="persistentBeanManager" class="net.sf.gilead.core.PersistentBeanManager">
<property name="proxyStore" ref="proxyStore"/>
<property name="persistenceUtil" ref="persistenceUtil"/>
</bean>
<!-- a Platfo开发者_运维百科rmTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
And here's my test file
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:**/applicationContext.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
public class UserDAOTest {
private static Logger log = Logger.getLogger(UserDAOTest.class);
@Autowired
private UserDAO userDAO;
@Test
public void getItemById() {
User user = null;
long id = 1;
user = userDAO.getItemById(id);
assertNotNull(user);
}
}
No matching bean of type [com.yeah.server.dao.UserDAO] found for dependency
Your component scan look stange:
<context:component-scan base-package="com.yeah.server.*" />
It should be a package and not a pattern.
Try: <context:component-scan base-package="com.yeah.server" />
it's a guess, but:
<context:component-scan base-package="com.yeah.server.*" />
should really be
<context:component-scan base-package="com.yeah.server" />
With out the .*
it isn't a path, just a package name
精彩评论