Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';我无法让JUnit使用@Autowired存储库和MongoTemplate_Java_Spring_Mongodb_Spring Mvc_Junit4 - Fatal编程技术网

Java Can';我无法让JUnit使用@Autowired存储库和MongoTemplate

Java Can';我无法让JUnit使用@Autowired存储库和MongoTemplate,java,spring,mongodb,spring-mvc,junit4,Java,Spring,Mongodb,Spring Mvc,Junit4,我在弄清楚如何在我的存储库上编写JUnit测试时遇到了一些问题,我希望在我的应用程序中自动连接该存储库。下面是存储库+测试类+XML+错误消息。基本上,我认为应用程序XML配置是不完整的,但我目前仍停留在需要添加的内容上。。。有人知道吗?谢谢 package org.jswiki.persistence; @Repository public class JSWikiRepository { @Autowired MongoTemplate mongoTemplate;

我在弄清楚如何在我的存储库上编写JUnit测试时遇到了一些问题,我希望在我的应用程序中自动连接该存储库。下面是存储库+测试类+XML+错误消息。基本上,我认为应用程序XML配置是不完整的,但我目前仍停留在需要添加的内容上。。。有人知道吗?谢谢

package org.jswiki.persistence;

@Repository
public class JSWikiRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    public JSWikiRepository() {

    }

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

“base package=“org.jswiki.persistencer”中存在拼写错误,而JSWikiRepository的包是org.jswiki.persistence

您的位置
”*/conf mongodb.xml
对我来说看起来很奇怪-来自此配置文件的其他bean的自动连接有效吗?自动连接适用于应用程序的其余部分,但不适用于单元测试。当我编写像/resources/spring/conf-mongodb.xml这样的东西时,没有找到xml文件,这是唯一一个让它工作的字符串。但它可能仍然是不正确的。哦,非常愚蠢,但如果我修复它仍然不起作用。拼写错误是因为我尝试了不同的东西。它会给出相同的错误吗?如果是,作为实验,请尝试在.xml文件中显式声明JSWikiRepository(并从此类中删除@Repository注释)。然后呢?
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);
    }
}
   <!-- 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.springframework.test.context.support.DependencyInjectionTestExecutionListener@39c0f4a] to prepare test instance [org.jswiki.MongoTest@1ce92674]
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]