2012-08-17 117 views
0

我是Spring MVC和JUnit的新手。基本上我想自動裝載服務類,這個類應該在春天的環境中加載。Spring 3和JUnit 4(自動裝配)

服務

 

    @Service 
    public class FundService { 
     @Autowired 
     FundDAO fundDAO; 

     /** 
     * @return 
     */ 
     public List getFundDetails(String productId) { 

      return fundDAO.getFundDetails(productId); 
     } 
    } 

應用上下文

<beans> 

    <mvc:annotation-driven /> 

    <context:component-scan base-package="com.test.*" /> 
</beans> 

JUnit類

 

    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(locations={"classpath*:/WEB-INF/application-context.xml"}) 
    public class CompensationServiceTest { 

     @Autowired 
     private FundService fundService; 

     @Test 
     public void verifyGetCompensationList() 
     { 
      System.out.println(fundService == null); 
     } 
    } 

在執行我得到以下異常跟蹤測試

 

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.admin.service.CompensationServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.admin.service.FundService com.test.admin.service.CompensationServiceTest.fundService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.admin.service.FundService] 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:286) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064) 
     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:333) 
     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:49) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
     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:236) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
     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: private com.test.admin.service.FundService com.test.admin.service.CompensationServiceTest.fundService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.admin.service.FundService] 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:507) 
     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 
     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283) 
     ... 26 more 
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.admin.service.FundService] 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:903) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:772) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:686) 
     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) 
     ... 28 more 

回答

2

首先,WEB-INF文件夾不應該在類路徑中;它應該在項目的文件系統中。因此,如果您使用Maven項目結構,該文件夾將相對於您的項目的根目錄(即,src/main/webapp/WEB-INF)。在這種情況下,你會想聲明該文件夾中的XML配置文件這樣的文件系統資源:

@ContextConfiguration("file:src/main/webapp/WEB-INF/application-context.xml")

其次,如果這是一個測試配置文件,你不應該將它存儲WEB-INF下。相反,您應該將其存儲在類路徑中。繼Maven項目結構,這將是src/test/resources/application-context.xml,在這種情況下,你可以使用下面的聲明在您的測試:

@ContextConfiguration("/application-context.xml")

@ContextConfiguration("classpath:application-context.xml")

......無論你喜歡,但請注意它們是相同的。

的問候和感謝,

山姆

0

您必須使用「@Component」註釋來限定Service類,以便在組件掃描期間選取它,對嗎?

+0

@Service也可以做同樣的事情。就像組件一樣,它也是立體聲類型之一。 – Ritesh 2012-08-17 09:44:27

0

你能後,你做你的bean配置的代碼。這裏的問題在於Spring試圖找到一個具體的類來連線

@Autowired 
private FundService fundService; 

您需要指定Spring應該在您的配置中注入的bean。例如,你可以這樣說:

<beans> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.test.*" /> 

    <!-- Assuming FundServiceImpl is a class that implement FundService --> 
    <bean id="fundService" class="com.test.admin.service.FundServiceImpl"/> 
</beans>