spring-framework / org.springframework.test.context

Package org.springframework.test.context

Types

TestExecutionListener

interface TestExecutionListener

TestExecutionListener defines a listener API for reacting to test execution events published by the TestContextManager with which the listener is registered.

Note that not all testing frameworks support all lifecycle callbacks defined in this API. For example, #beforeTestExecution and #afterTestExecution are not supported in conjunction with JUnit 4 when using the org.springframework.test.context.junit4.rules.SpringMethodRule.

This interface provides empty default implementations for all methods. Concrete implementations can therefore choose to override only those methods suitable for the task at hand.

Concrete implementations must provide a public no-args constructor, so that listeners can be instantiated transparently by tools and configuration mechanisms.

Implementations may optionally declare the position in which they should be ordered among the chain of default listeners via the org.springframework.core.Ordered interface or org.springframework.core.annotation.Order annotation. See TestContextBootstrapper#getTestExecutionListeners() for details.

Spring provides the following out-of-the-box implementations (all of which implement Ordered):

  • org.springframework.test.context.web.ServletTestExecutionListener
  • org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
  • org.springframework.test.context.support.DependencyInjectionTestExecutionListener
  • org.springframework.test.context.support.DirtiesContextTestExecutionListener
  • org.springframework.test.context.transaction.TransactionalTestExecutionListener
  • org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener

Annotations

BootstrapWith

class BootstrapWith

@BootstrapWith defines class-level metadata that is used to determine how to bootstrap the Spring TestContext Framework.

This annotation may also be used as a meta-annotation to create custom composed annotations.

ContextHierarchy

class ContextHierarchy

@ContextHierarchy is a class-level annotation that is used to define a hierarchy of org.springframework.context.ApplicationContext for integration tests. Examples

The following JUnit-based examples demonstrate common configuration scenarios for integration tests that require the use of context hierarchies.

Single Test Class with Context Hierarchy

ControllerIntegrationTests represents a typical integration testing scenario for a Spring MVC web application by declaring a context hierarchy consisting of two levels, one for the root WebApplicationContext (with TestAppConfig) and one for the dispatcher servlet WebApplicationContext (with WebConfig). The WebApplicationContext that is autowired into the test instance is the one for the child context (i.e., the lowest context in the hierarchy).

 @RunWith(SpringRunner.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = TestAppConfig.class), @ContextConfiguration(classes = WebConfig.class) }) public class ControllerIntegrationTests { @Autowired private WebApplicationContext wac; // ... }
Class Hierarchy with Implicit Parent Context

The following test classes define a context hierarchy within a test class hierarchy. AbstractWebTests declares the configuration for a root WebApplicationContext in a Spring-powered web application. Note, however, that AbstractWebTests does not declare @ContextHierarchy; consequently, subclasses of AbstractWebTests can optionally participate in a context hierarchy or follow the standard semantics for @ContextConfiguration. SoapWebServiceTests and RestWebServiceTests both extend AbstractWebTests and define a context hierarchy via @ContextHierarchy. The result is that three application contexts will be loaded (one for each declaration of @ContextConfiguration, and the application context loaded based on the configuration in AbstractWebTests will be set as the parent context for each of the contexts loaded for the concrete subclasses.

 @RunWith(SpringRunner.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml") public abstract class AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml") public class SoapWebServiceTests extends AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/rest-ws-config.xml") public class RestWebServiceTests extends AbstractWebTests {}
Class Hierarchy with Merged Context Hierarchy Configuration

The following classes demonstrate the use of named hierarchy levels in order to merge the configuration for specific levels in a context hierarchy. BaseTests defines two levels in the hierarchy, parent and child. ExtendedTests extends BaseTests and instructs the Spring TestContext Framework to merge the context configuration for the child hierarchy level, simply by ensuring that the names declared via ContextConfiguration#name are both "child". The result is that three application contexts will be loaded: one for "/app-config.xml", one for "/user-config.xml", and one for {"/user-config.xml", "/order-config.xml"}. As with the previous example, the application context loaded from "/app-config.xml" will be set as the parent context for the contexts loaded from "/user-config.xml" and {"/user-config.xml", "/order-config.xml"}.

 @RunWith(SpringRunner.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/order-config.xml") ) public class ExtendedTests extends BaseTests {}
Class Hierarchy with Overridden Context Hierarchy Configuration

In contrast to the previous example, this example demonstrates how to override the configuration for a given named level in a context hierarchy by setting the ContextConfiguration#inheritLocations flag to false. Consequently, the application context for ExtendedTests will be loaded only from "/test-user-config.xml" and will have its parent set to the context loaded from "/app-config.xml".

 @RunWith(SpringRunner.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/test-user-config.xml", inheritLocations = false) ) public class ExtendedTests extends BaseTests {}

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.