2014-10-18 21 views
1

我在編寫如何在存儲庫上編寫JUnit測試時遇到了一些麻煩,我想在應用程序中自動裝配它。我有下面的存儲庫+測試類+ XML +錯誤消息。基本上我認爲應用程序的XML配置是不完整的,但我暫時停留在我需要添加的東西上...任何人有任何想法?謝謝。無法使用JUnit與我的@Autowired存儲庫和MongoTemplate配合工作

package org.jswiki.persistence; 

@Repository 
public class JSWikiRepository { 

    @Autowired 
    MongoTemplate mongoTemplate; 

    public JSWikiRepository() { 

    } 

    public List<JSWikiItem> getAll() { 
     return mongoTemplate.findAll(JSWikiItem.class); 
    } 
} 

這是我的測試類:

package org.jswiki; 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"*/conf-mongodb.xml"}) 
public class MongoTest { 

    @Autowired 
    private JSWikiRepository jsRep; 

    @Test 
    public void canCreateNewRecord() { 

     List<JSWikiItem> listWiki = jsRep.getAll(); 

     //for(JSWikiItem i : listWiki) { 
     // System.out.println(i.getTitle()); 
     //} 

     assertEquals(1, 1); 
    } 
} 

這是我的XML

<!-- Activate annotation configured components --> 
    <context:annotation-config/> 

    <!-- Scan components for annotations within the configured package --> 
    <context:component-scan base-package="org.jswiki.controller" /> 
    <context:component-scan base-package="org.jswiki.domain" /> 
    <context:component-scan base-package="org.jswiki.persistencer" /> 

    <!-- Define the MongoTemplate which handles connectivity with MongoDB --> 
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg name="mongo" ref="mongo"/> 
    <constructor-arg name="databaseName" value="jswiki"/> 
    </bean> 

    <!-- Factory bean that creates the Mongo instance --> 
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean"> 
    <property name="host" value="178.62.218.109"/> 
    </bean> 

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes --> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

錯誤

09:52:36.520 [main] DEBUG o.s.b.f.annotation.InjectionMetadata - Processing injected method of bean 'org.jswiki.MongoTest': AutowiredFieldElement for private org.jswiki.persistence.JSWikiRepository org.jswiki.MongoTest.jsRep 
09:52:36.527 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]39c0f4a] to prepare test instance [[email protected]] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.jswiki.MongoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jswiki.persistence.JSWikiRepository org.jswiki.MongoTest.jsRep; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.jswiki.persistence.JSWikiRepository] 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)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE] 
+0

您的位置'「*/conf-mongodb.xml'對我來說看起來很奇怪 - 是否從這個配置文件中自動裝配其他bean? – Ralph 2014-10-18 08:27:28

+0

自動裝配適用於其他應用程序,而不是在單元測試中。當我編寫類似/resources/spring/conf-mongodb.xml的文件時,找不到xml文件,這是唯一能夠正常工作的字符串,但它可能仍然不正確。 – mickske 2014-10-18 14:20:49

回答

0

你有「基地拼寫錯誤的開始-package =「org.jswiki。persistencer',而JSWikiRepository的包是org.jswiki.persistence

+0

糟糕,但如果我修復它仍然不起作用。拼寫錯誤是因爲我嘗試了不同的東西 – mickske 2014-10-18 11:48:25

+0

它是否會給出相同的錯誤?如果是,就像實驗一樣,嘗試在.xml文件中明確聲明JSWikiRepository(並刪除@Repository這個類的註釋)然後會發生什麼? – Alexander 2014-10-18 12:09:43

相關問題