为什么应用程序上下文无法加载?

时间:2013-01-07 12:09:06

标签: spring-mvc maven-3 junit4

任何机构都可以解释为什么我在测试我的测试用例时会收到以下错误消息“无法加载ApplicationContext”

 mvn test

3 个答案:

答案 0 :(得分:2)

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

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

}

答案 1 :(得分:1)

检查你的@ContextConfiguration。引自Spring documentation

  

将处理普通或相对路径 - 例如“context.xml”   作为一个类路径资源,它相对于其中的包   测试类已定义。以斜杠开头的路径被视为   绝对类路径位置,例如“/org/example/config.xml”。一个   表示资源URL的路径(即,前缀为的路径)   classpath:,file:,http:等等)将按原样使用。

所以我认为你必须使用“/applicationContext.xml”而不是“/src/test/resources/applicationContext.xml”,因为文件将放在类路径的根级别。

答案 2 :(得分:0)

由于我看不到您的配置,您可以更改您的测试以使用以下类级别注释。这应该有助于找到应用程序上下文,只要它在类路径中。

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