[英]Can't get JUnit working with my @Autowired repository and MongoTemplate
I'm having some trouble figuring out how to write a JUnit test on my Repository, which I want to be autowired within my application. 我在弄清楚如何在我的存储库上编写JUnit测试时遇到了一些麻烦,我想将其自动连接到应用程序中。 I have the repository + test class + XML + error message below. 我在下面有存储库+测试类+ XML +错误消息。 Basically I think the application XML config is incomplete but I'm stuck at the moment on what I need to add... anyone have any idea? 基本上,我认为应用程序XML配置是不完整的,但目前我在需要添加的内容上受阻……任何人有任何想法吗? Thanks. 谢谢。
package org.jswiki.persistence;
@Repository
public class JSWikiRepository {
@Autowired
MongoTemplate mongoTemplate;
public JSWikiRepository() {
}
public List<JSWikiItem> getAll() {
return mongoTemplate.findAll(JSWikiItem.class);
}
}
This is my test 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);
}
}
This is my XML 这是我的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"/>
Start of the error 错误开始
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]
你必须在“基本包=“org.jswiki。persistencer”拼写错误,而JSWikiRepository的包是org.jswiki.persistence。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.