2012-09-27 115 views
2

我正在嘗試爲存儲庫創建junit測試,我收到了這個很好的例外。嘗試創建測試時注入自動連接的依賴關係失敗

完全例外:

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 

我有@Repository到位

@Repository 
public class RepositoryJDBC implements RepositoryInterface { 

我有一個ApplicationContext-的test.xml設置:

<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"/> 

,這裏是實際junit測試:

@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()); 
    } 
+0

我相信在這個項目中某處沒有設置正確的東西。這是一個繼承的項目。好極了。 –

回答

1

有時需要bean定義,答案是關閉Eclipse/STS,刪除〜/ .m2/repository目錄,然後重新啓動Eclipse。

+0

有另一位同事在從svn抓取它之後在他的機器上看了這個。它工作得很好。很奇怪。 –

2

進樣的接口,而不是具體的類:

@Autowired RepositoryInterface testRepository; 

它將工作,因爲一旦你申請@Repository註釋,春包裝你原來RepositoryJDBC類的代理。默認情況下,這個代理實現了具體類已經實現的所有接口 - 但不擴展該類。

+0

我試過這個,我得到了同樣的異常,接受了RepositoryInterface而不是RepositoryJDBC的匹配bean。我在接口上應用了'@ Repository'註釋,並從RepositoryJDBC中移除它。然後我把它換回來,仍然有錯誤。每次運行測試前,我都要確保清理項目。 –

+1

@Kodefoo:你確定'RepositoryJDBC'被創建(在構造函數中放置一個斷點)?它沒有'@ Repository'註釋工作嗎? –

+0

我在構造函數中添加了一個println,它正在輸出中顯示。我也清理並建造了這個項目。刪除註釋使我獲得相同的結果。 –

0

我認爲<mvc:annotation-driven /><context:annotation-config />相沖突,然後Repository bean沒有被創建。

刪除<mvc:annotation-driven />和測試(它的作品我試了一下)

另外,當您遇到奇怪的錯誤,你不應該從配置文件

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

刪除了''並刪除了''相同的結果。非常令人沮喪。 –

相關問題