Spring 3 单元测试
我正在尝试为我的 DAO 类编写一个单元测试,但问题是我收到此错误:
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)}
这是我的 applicationContext 文件
<?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 PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
这是我的测试文件
@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);
}
}
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 PlatformTransactionManager 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的组件扫描看起来很奇怪:
它应该是一个包而不是一个模式。
尝试:
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:
should really be
With out the
.*
it isn't a path, just a package name