2013-01-07 76 views
0

任何機構可以解釋爲什麼我應該得到以下錯誤消息「無法加載的ApplicationContext」,而如果你使用Spring框架測試我的測試案例爲什麼應用程序上下文無法加載?

mvn test 
+2

我們需要看你的測試類和配置。 – cowls

+0

閱讀堆棧跟蹤,你應該知道發生了什麼。它可能是任何彈簧在讀取appcontext xml時會跳過的內容,如無效的xml,或指定的未找到的類或在bean上指定的無效屬性。 –

回答

2

,你可以指定你的applicationContext.xml文件的位置使用@ContextConfiguration批註:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/path/to/your/applicationContext.xml" }) 
public class MyTest { 

} 
+0

即使配置@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {「/src/test/resources/applicationContext.xml」})我的問題沒有解決 – sivaram

+0

同樣的錯誤類型「application context.xml」不能被加載 – sivaram

0

由於我看不到您的配置,您可以將您的測試更改爲使用以下課程級別註釋。只要它在類路徑中,這應該有助於查找應用程序上下文。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ "classpath:applicationContext.xml" }) 
1

檢查您的@ContextConfiguration。從Spring documentation引用:

的滑動或相對路徑 - 例如「context.xml中」 - 將被視爲 作爲類路徑的資源,相對於在其中 測試類定義的包。以斜槓開始的路徑被視爲絕對類路徑位置,例如「/org/example/config.xml」。代表資源URL(即,以 classpath :, file:,http:等爲前綴的路徑)的路徑將被原樣使用。

,所以我想你必須使用「/applicationContext.xml」而不是「/src/test/resources/applicationContext.xml」作爲文件將被放置在你的類路徑的根水平。

相關問題