正如 duffymo 所暗示的那样,Spring TestContext Framework (TCF) 假定默认情况下字符串位置位于类路径中。有关详细信息,请参阅用于上下文配置的 JavaDoc。
但是,请注意,您也可以使用Spring的资源抽象(即,通过使用“file:”前缀)使用绝对或相对路径在文件系统中指定资源。您可以在 JavaDoc 中找到有关此内容的详细信息,例如 Spring 的 modifyLocations() 方法。AbstractContextLoader
例如,如果 XML 配置文件位于项目文件夹中,则可以将位置指定为相对文件系统路径,如下所示:"src/main/webapp/WEB-INF/spring-config.xml"
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")
作为替代方法,您可以将Spring配置文件存储在类路径中(例如,),然后通过Spring MVC配置中的类路径引用它们 - 例如:src/main/resources
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
使用此方法时,测试配置将如下所示(请注意前导斜杠,它表示资源位于类路径的根目录中):
@ContextConfiguration("/spring-config.xml")
您可能还会发现参考手册的 XML 资源的上下文配置部分很有用。
问候
山 姆
(Spring TestContext Framework的作者)