This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.2.0!

@WebAppConfiguration

@WebAppConfiguration is a class-level annotation that you can use to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext. The mere presence of @WebAppConfiguration on a test class ensures that a WebApplicationContext is loaded for the test, using the default value of "file:src/main/webapp" for the path to the root of the web application (that is, the resource base path). The resource base path is used behind the scenes to create a MockServletContext, which serves as the ServletContext for the test’s WebApplicationContext.

The following example shows how to use the @WebAppConfiguration annotation:

  • Java

  • Kotlin

@ContextConfiguration
@WebAppConfiguration (1)
class WebAppTests {
	// class body...
}
1 The @WebAppConfiguration annotation.
@ContextConfiguration
@WebAppConfiguration (1)
class WebAppTests {
	// class body...
}
1 The @WebAppConfiguration annotation.

To override the default, you can specify a different base resource path by using the implicit value attribute. Both classpath: and file: resource prefixes are supported. If no resource prefix is supplied, the path is assumed to be a file system resource. The following example shows how to specify a classpath resource:

  • Java

  • Kotlin

@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") (1)
class WebAppTests {
	// class body...
}
1 Specifying a classpath resource.
@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") (1)
class WebAppTests {
	// class body...
}
1 Specifying a classpath resource.

Note that @WebAppConfiguration must be used in conjunction with @ContextConfiguration, either within a single test class or within a test class hierarchy. See the @WebAppConfiguration javadoc for further details.