簡體   English   中英

為什么無法加載應用程序上下文?

[英]why application context cannot be loaded?

任何機構都可以解釋為什么在測試我的測試用例時為什么會出現以下錯誤消息“無法加載ApplicationContext”

 mvn test

如果使用Spring框架,則可以使用@ContextConfiguration批注指定applicationContext.xml文件的位置:

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

}

檢查您的@ContextConfiguration。 引用Spring文檔

純路徑或相對路徑(例如“ context.xml”)將被視為相對於定義測試類的程序包的類路徑資源。 以斜杠開頭的路徑被視為絕對類路徑位置,例如“ /org/example/config.xml”。 表示資源URL的路徑(即以classpath:,file:,http:等開頭的路徑)將照原樣使用。

因此,我想您必須使用“ /applicationContext.xml ”而不是“ /src/test/resources/applicationContext.xml ”,因為該文件將放置在類路徑的根目錄下。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM