Paul Parker
Paul Parker

Reputation: 467

Injection of autowired dependencies failed when trying to create a test

I am trying create a junit test for a repository and I am getting this nice exception.

Full Exception:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gov.mt.dphhs.sdnh.core.domain.RepositoryJDBCTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC gov.mt.dphhs.sdnh.core.domain.RepositoryJDBCTest.testRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC] 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:285)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC gov.mt.dphhs.sdnh.core.domain.RepositoryJDBCTest.testRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    ... 27 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 29 more

I have the @Repository in place

@Repository
public class RepositoryJDBC implements RepositoryInterface {

I have the applicationContext-test.xml setup:

<context:annotation-config />
    <context:component-scan base-package="gov.mt.dphhs.sdnh.core" />
    <context:component-scan base-package="gov.mt.dphhs.sdnh.web" />
    <context:component-scan base-package="gov.mt.dphhs.sdnh.core.domain.impl" />
    <mvc:annotation-driven />

    <jee:jndi-lookup id="testDataSource" jndi-name="jdbc/newHireDS" />

    <!-- 
        Setup Repositories
    --> 
    <bean id="repositoryJDBC"
          class="gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC"/>

and here is the actual junit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/applicationContext-test.xml" })
public class RepositoryJDBCTest {

    @Autowired RepositoryJDBC testRepository;


    private Employee testEmployee;
    private Employer testEmployer;
    private List<Employee> employeeList;
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @BeforeClass
    public static void setUpClass() throws Exception {
        // jndi connection information removed
    }

    /**
     * 
     * @throws Exception
     */
    @Test
    public void testFindEmployee() throws Exception {
        System.out.println("Find Employee test: ");
        /* Start the Test */
        /* Get the test employee and check that the Employee object is created */
        testEmployee = testRepository.findEmployee(123940); // (Integer)

        assertNotNull(testEmployee);

        System.out.println("\t found this employee: " + testEmployee.toString());
    }

Upvotes: 2

Views: 8868

Answers (3)

tgharold
tgharold

Reputation: 731

Sometimes when you run into strange errors, the answer is to close Eclipse/STS, delete your ~/.m2/repository directory, and then restart Eclipse.

Upvotes: 1

Adolfo
Adolfo

Reputation: 838

I think that the <mvc:annotation-driven /> is conflicting with <context:annotation-config /> and then the Repository bean is not being created.

Remove <mvc:annotation-driven /> and test (it works I tried it)

Plus you should not need the bean definition from the config file

<bean id="repositoryJDBC" class="gov.mt.dphhs.sdnh.core.domain.impl.RepositoryJDBC"/>

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340973

Inject interface rather than concrete class:

@Autowired RepositoryInterface testRepository;

It will work because once you apply @Repository annotation, Spring wraps your original RepositoryJDBC class with a proxy. By default this proxy implements all interfaces already implemented by the concrete class - but does not extend that class.

Upvotes: 2

Related Questions