正如duffymo所暗示的那样,Spring TestContext
framework(TCF)假定默认情况下字符串位置在类路径中。有关详细信息,请参见ContextConfiguration的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配置文件存储在类路径(例如
src/main/resources)中,然后通过Spring MVC配置中的类路径引用它们-例如:
<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的作者)



