2015-06-05 32 views
0

在JUnit測試獲得以下錯誤消息:春告訴我一個bean的名稱沒有定義,當它是

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'maintenanceEmailSupplier' is defined 

完整版:

16:36:47,707 INFO TransactionContext:136 - Rolled back transaction for test context [[email protected] testClass = FeedbackControllerTest, testInstance = [email protected]1, testMethod = [email protected], testException = org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'maintenanceEmailSupplier' is defined, mergedContextConfiguration = [[email protected] testClass = FeedbackControllerTest, locations = '{classpath*:WEB-INF/spring/app-config.xml, classpath*:WEB-INF/spring/mvc-config.xml, classpath*:WEB-INF/spring/security-config.xml, classpath:/dao-context.xml, classpath:/envDatasource-context.xml, classpath:/jndiDatasource-config.xml, classpath*:WEB-INF/spring/app-config.xml, classpath*:WEB-INF/spring/mvc-config.xml, classpath*:WEB-INF/spring/security-config.xml, classpath:/dao-context.xml, classpath:/envDatasource-context.xml, classpath:/jndiDatasource-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]. 

這是在頂部類:

@RunWith (SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@Transactional 
@TransactionConfiguration 
@ContextConfiguration ({"classpath*:WEB-INF/spring/app-config.xml", "classpath*:WEB-INF/spring/mvc-config.xml", 
         "classpath*:WEB-INF/spring/security-config.xml", 
         "/dao-context.xml", "/envDatasource-context.xml", "/jndiDatasource-config.xml"}) 

這是在app-config.xml中:

<bean id="maintenanceEmailSupplier" class="edu.mayo.lpea.ca.common.ForwardingSupplier"> 
    <constructor-arg value="#{appPropertyDao.getSupplier('MAINTENANCE_EMAIL')}"/> 
</bean> 

這是我打電話的代碼:

return applicationContext.getBean ("maintenanceEmailSupplier", Supplier.class); 

我缺少什麼?爲什麼不找到我的豆子? (請注意,刪除類說明符並不能解決問題。)

+0

是'WEB-INF /春/ APP-config.xml'測試運行的classpath的一部分嗎? –

+0

是的。當我嘗試運行單元測試時,該文件中的錯誤會導致問題 –

回答

0

您提供的信息對我來說還不夠,因爲它可能無法加載上下文文件或可以加載上下文,但無法初始化bean或缺少libs,某些Spring的命名空間。另一方面,你應該使用兩個獨立的環境。我的意思是一個用於測試,另一個用於生產。

所以,你應該嘗試下面將文件移到的src /測試/資源:

  • WEB-INF /春/ APP-config.xml中」,WEB-INF /春/ MVC-config.xml中「
    WEB-INF /春/安全-config.xml中」,/dao-context.xml,
    /envDatasource-context.xml,/jndiDatasource-config.xml

,改變你的註釋中測試類如下:

@RunWith (SpringJUnit4ClassRunner.class) 
@Transactional 
@ContextConfiguration ({"classpath*:/app-config.xml", "classpath*:/mvc-config.xml", 
         "classpath*:/security-config.xml", 
         "/dao-context.xml", "/envDatasource-context.xml", "/jndiDatasource-config.xml"}) 

讓我知道,如果我的想法可以幫助你,

問候

+0

您有擺脫WebAppConfiguration和TransactionConfiguration的原因嗎? –

+0

除非和爲了調試目的我需要對WEB-INF/*。xml文件進行更改,否則將它們放在兩個地方僅僅意味着我冒着在一個地方修復錯誤的風險不會讓它變成另一個地方地點。沒有? –

相關問題