2011-05-24 15 views
1

私はDAOクラスのユニットテストを書くことをしようとしているが、事は、私はこのエラーを取得することです:春の3ユニットテスト

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); 

} 
} 
+1

、単にパッケージ名パスすることはできない.*うちで

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

が本当に

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

する必要がありますあなたはUserDAOを投稿しますか?また、スタックトレースの書式を修正することができれば、おそらく助けになるでしょう。 – abalogh

答えて

7

No matching bean of type [com.yeah.server.dao.UserDAO] found for dependency

あなたのコンポーネントスキャンLO ok stange: <context:component-scan base-package="com.yeah.server.*" />

パターンではなくパッケージである必要があります。 試してみてください。<context:component-scan base-package="com.yeah.server" />

+0

まさにそのため、* base * -packageと呼ばれています。スキャンが始まるパッケージです。 –

4

それは推測だが、:それは

+0

も、同じ問題が残っています。 – karq

+1

まあ、どこかにあります。アプリケーションコンテキストは、UserDAOを実装するBeanを検出しません。選択肢は非常にスリムで、 'com.yeah.server'にないか、' @ Component'' @ Repository'で注釈されていないので、コンポーネントスキャンでは無視されます。 –

関連する問題