无法将Mongo存储库注入我的测试类

时间:2015-02-26 05:18:00

标签: spring-data spring-data-mongodb

尝试将Mongo存储库注入测试时获得以下异常

class.org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sg.tutswheel.test.repositories.CustomerRepository': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sg.tutswheel.test.repositories.CustomerRepository com.sg.tutswheel.test.repositories.CustomerRepository.customerRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sg.tutswheel.test.repositories.CustomerRepository] 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:334) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) ~[spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]"

的Junit

@ContextConfiguration(locations{"classpath:application/springcontexts/appContext-persistence.xml"})
public class CustomerRepository extends AbstractTestUnit{
        @Autowired 
        CustomerRepository customerRepository;
        @Test
        public void testContextLoader() {
        //....
        }
}

appContext-的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

  <mongo:mongo id="mongo" />

  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo" />
    <constructor-arg value="databaseName" />
  </bean>

  <mongo:repositories base-package="com.sg.tutswheel.persistence.repositories" mongo-template-ref="mongoTemplate"  />

</beans>

CustomerRepository

public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long>{
        Customer findByFirstName(String firstName);
    }

Customer.java

@Document
public class Customer extends AbstractEntity{

    private String firstName, lastName;

    public Customer(String firstName, String lastName) {

        Assert.hasText(firstName,"First Name cannot be null or empty");
        Assert.hasText(lastName, "Last Name cannot be null or empty");

        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:3)

我认为问题是您的测试类名称是 CustomerRepository - 与您正在测试的真实CustomerRepository相同。

因此,Spring正在尝试查找测试类类型的bean(而不是存储库类型),但无法找到它。

尝试将测试类重命名为不同的名称。例如:

@ContextConfiguration(locations{"classpath:application/springcontexts/appContext-persistence.xml"})
public class CustomerRepositoryTest extends AbstractTestUnit{
        @Autowired 
        CustomerRepository customerRepository;
        @Test
        public void testContextLoader() {
        //....
        }
}