Sudhanva Gnaneshwar
Sudhanva Gnaneshwar

Reputation: 3

Unable to inject Mongo Repository into my test class

Getting below exception when trying to inject Mongo repository into test

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

}

Thanks in advance.

Upvotes: 0

Views: 1667

Answers (1)

Igor Uzhviev
Igor Uzhviev

Reputation: 308

I think the problem is that your test class name is CustomerRepository - the same as you real CustomerRepository which you are testing.

Therefore Spring is trying to find a bean of your test class type (not the repository type) and cannot find it.

Try to rename test class to something different. For example:

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

Upvotes: 3

Related Questions