我在如何在我的存储库上编写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]
答案 0 :(得分:0)
您在&#39; base-package =&#34; org.jswiki。 persistencer &#39;中有拼写错误,而JSWikiRepository的包是 org.jswiki .persistence 即可。