Spring 3 单元测试

发布于 2024-11-09 13:38:30 字数 4796 浏览 0 评论 0原文

我正在尝试为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-11-16 13:38:30

找不到依赖项的 [com.yeah.server.dao.UserDAO] 类型的匹配 bean

您的组件扫描看起来很奇怪:

它应该是一个包而不是一个模式。
尝试:

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" />

计㈡愣 2024-11-16 13:38:30

这是一个猜测,但是:

<context:component-scan base-package="com.yeah.server.*" />

应该是

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文