【问题标题】:Can't get JUnit working with my @Autowired repository and MongoTemplate无法让 JUnit 与我的 @Autowired 存储库和 MongoTemplate 一起使用
【发布时间】:2014-10-18 07:59:29
【问题描述】:

我在弄清楚如何在我的存储库上编写 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);
    }
}

这是我的测试课:

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.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]

【问题讨论】:

  • 你的位置 "*/conf-mongodb.xml 对我来说看起来很奇怪 - 从这个配置文件自动装配其他 bean 是否有效?
  • 自动装配适用于应用程序的其余部分,但不适用于单元测试。当我写 /resources/spring/conf-mongodb.xml 之类的东西时,找不到 xml 文件,这是使它工作的唯一字符串。但它可能仍然不正确。

标签: java spring mongodb spring-mvc junit4


【解决方案1】:

'base-package="org.jswiki.persistencer' 中有拼写错误,而 JSWikiRepository 的包是 org.jswiki.persistence

【讨论】:

  • 糟糕,非常愚蠢,但如果我修复它,它仍然不起作用。出现拼写错误是因为我尝试了不同的东西。
  • 它会给出同样的错误吗?如果是,作为一个实验,尝试在 .xml 文件中显式声明 JSWikiRepository(并从此类中删除 @Repository 注释)。然后会发生什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2018-05-09
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多