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);
}
}
没有找到相关结果
已邀请:
2 个回复
才脊烽馈低
它应该是一个包而不是模式。 试试:
嗓瑰
应该真的是
如果没有ѭ7,它不是路径,而只是包名