Location of spring-context.xml

As duffymo hinted at, the Spring TestContext Framework (TCF) assumes that string locations are in the classpath by default. For details, see the JavaDoc for ContextConfiguration.

Note, however, that you can also specify resources in the file system with either an absolute or relative path using Spring’s resource abstraction (i.e., by using the “file:” prefix). You can find details on that in the JavaDoc for the modifyLocations() method in Spring’s AbstractContextLoader.

So for example, if your XML configuration file is located in "src/main/webapp/WEB-INF/spring-config.xml" in your project folder, you could specify the location as a relative file system path as follows:

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")

As an alternative, you could store your Spring configuration files in the classpath (e.g., src/main/resources) and then reference them via the classpath in your Spring MVC configuration — for example:

<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>

With this approach, your test configuration would simply look like this (note the leading slash that denotes that the resource is in the root of the classpath):

@ContextConfiguration("/spring-config.xml")

You might also find the Context configuration with XML resources section of the reference manual useful.

Regards,

Sam

(author of the Spring TestContext Framework)

Leave a Comment