2012-12-05 19 views
1

我遇到了嚴重問題,試圖用Spring運行JUnit測試。使用JUnit測試Spring應用程序的問題:僅在測試中引發NoSuchBeanDefinitionException

這裏是我的JUnit類配置:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext*.xml", "file:src/main/webapp/WEB-INF/spring/webmvc-config.xml" }) 

applicationContext*.xml文件,我做一個有這樣的:

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <beans:property name="basenames"> 
      <beans:list> 
       <beans:value>messagesSecurity</beans:value> 
      </beans:list> 
     </beans:property> 
    </beans:bean> 

請注意,我的應用程序工作正常,當我從Tomcat的運行它。這是只有當我運行JUnit測試,我得到NoSuchBeanDefinitionException如下:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.context.support.ResourceBundleMessageSource] 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:924) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) 
    ... 118 more 

我感到相當這種行爲百思不得其解......誰能請指教?

回答

3

我終於找到了我的問題的原因:我使用的是消息源類,而不是使用接口造成代理問題。

重構我的應用程序以使用MessageSource接口對問題進行排序。

查看完整對話here在Spring論壇上。