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

Unable to inject Mongo Repository into my test class

尝试将 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.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]"

联合

@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;
    }

}

提前致谢。

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

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

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

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