Testing
This chapter covers Spring’s support for integration testing and best practices for unit testing. The Spring team advocates test-driven development (TDD). The Spring team has found that the correct use of inversion of control (IoC) certainly does make both unit and integration testing easier (in that the presence of setter methods and appropriate constructors on classes makes them easier to wire together in a test without having to set up service locator registries and similar structures).
1. Introduction to Spring Testing
Testing is an integral part of enterprise software development. This chapter focuses on the value added by the IoC principle to unit testing and on the benefits of the Spring Framework’s support for integration testing. (A thorough treatment of testing in the enterprise is beyond the scope of this reference manual.)
2. Unit Testing
Dependency injection should make your code less dependent on the container than it would be with traditional Java EE development. The POJOs that make up your application should be testable in JUnit or TestNG tests, with objects instantiated by using the new
operator, without Spring or any other container. You can use mock objects (in conjunction with other valuable testing techniques) to test your code in isolation. If you follow the architecture recommendations for Spring, the resulting clean layering and componentization of your codebase facilitate easier unit testing. For example, you can test service layer objects by stubbing or mocking DAO or repository interfaces, without needing to access persistent data while running unit tests.
True unit tests typically run extremely quickly, as there is no runtime infrastructure to set up. Emphasizing true unit tests as part of your development methodology can boost your productivity. You may not need this section of the testing chapter to help you write effective unit tests for your IoC-based applications. For certain unit testing scenarios, however, the Spring Framework provides mock objects and testing support classes, which are described in this chapter.
2.1. Mock Objects
Spring includes a number of packages dedicated to mocking:
2.1.1. Environment
The org.springframework.mock.env
package contains mock implementations of the Environment
and PropertySource
abstractions (see Bean Definition Profiles and PropertySource
Abstraction). MockEnvironment
and MockPropertySource
are useful for developing out-of-container tests for code that depends on environment-specific properties.
2.1.2. JNDI
The org.springframework.mock.jndi
package contains an implementation of the JNDI SPI, which you can use to set up a simple JNDI environment for test suites or stand-alone applications. If, for example, JDBC DataSource
instances get bound to the same JNDI names in test code as they do in a Java EE container, you can reuse both application code and configuration in testing scenarios without modification.
2.1.3. Servlet API
The org.springframework.mock.web
package contains a comprehensive set of Servlet API mock objects that are useful for testing web contexts, controllers, and filters. These mock objects are targeted at usage with Spring’s Web MVC framework and are generally more convenient to use than dynamic mock objects (such as EasyMock ) or alternative Servlet API mock objects (such as MockObjects ).
Since Spring Framework 5.0, the mock objects in org.springframework.mock.web are based on the Servlet 4.0 API. |
The Spring MVC Test framework builds on the mock Servlet API objects to provide an integration testing framework for Spring MVC. See Spring MVC Test Framework.
2.1.4. Spring Web Reactive
The org.springframework.mock.http.server.reactive
package contains mock implementations of ServerHttpRequest
and ServerHttpResponse
for use in WebFlux applications. The org.springframework.mock.web.server
package contains a mock ServerWebExchange
that depends on those mock request and response objects.
Both MockServerHttpRequest
and MockServerHttpResponse
extend from the same abstract base classes as server-specific implementations and share behavior with them. For example, a mock request is immutable once created, but you can use the mutate()
method from ServerHttpRequest
to create a modified instance.
In order for the mock response to properly implement the write contract and return a write completion handle (that is, Mono<Void>
), it by default uses a Flux
with cache().then()
, which buffers the data and makes it available for assertions in tests. Applications can set a custom write function (for example, to test an infinite stream).
The WebTestClient builds on the mock request and response to provide support for testing WebFlux applications without an HTTP server. The client can also be used for end-to-end tests with a running server.
2.2. Unit Testing Support Classes
Spring includes a number of classes that can help with unit testing. They fall into two categories:
2.2.1. General Testing Utilities
The org.springframework.test.util
package contains several general purpose utilities for use in unit and integration testing.
ReflectionTestUtils
is a collection of reflection-based utility methods. You can use these methods in testing scenarios where you need to change the value of a constant, set a non-public
field, invoke a non-public
setter method, or invoke a non-public
configuration or lifecycle callback method when testing application code for use cases such as the following:
-
ORM frameworks (such as JPA and Hibernate) that condone
private
orprotected
field access as opposed topublic
setter methods for properties in a domain entity. -
Spring’s support for annotations (such as
@Autowired
,@Inject
, and@Resource
), that provide dependency injection forprivate
orprotected
fields, setter methods, and configuration methods. -
Use of annotations such as
@PostConstruct
and@PreDestroy
for lifecycle callback methods.
AopTestUtils
is a collection of AOP-related utility methods. You can use these methods to obtain a reference to the underlying target object hidden behind one or more Spring proxies. For example, if you have configured a bean as a dynamic mock by using a library such as EasyMock or Mockito, and the mock is wrapped in a Spring proxy, you may need direct access to the underlying mock to configure expectations on it and perform verifications. For Spring’s core AOP utilities, see AopUtils
and AopProxyUtils
.
2.2.2. Spring MVC Testing Utilities
The org.springframework.test.web
package contains ModelAndViewAssert
, which you can use in combination with JUnit, TestNG, or any other testing framework for unit tests that deal with Spring MVC ModelAndView
objects.
Unit testing Spring MVC Controllers
To unit test your Spring MVC Controller classes as POJOs, use ModelAndViewAssert combined with MockHttpServletRequest , MockHttpSession , and so on from Spring’s Servlet API mocks. For thorough integration testing of your Spring MVC and REST Controller classes in conjunction with your WebApplicationContext configuration for Spring MVC, use the Spring MVC Test Framework instead. |
3. Integration Testing
This section (most of the rest of this chapter) covers integration testing for Spring applications. It includes the following topics:
3.1. Overview
It is important to be able to perform some integration testing without requiring deployment to your application server or connecting to other enterprise infrastructure. Doing so lets you test things such as:
-
The correct wiring of your Spring IoC container contexts.
-
Data access using JDBC or an ORM tool. This can include such things as the correctness of SQL statements, Hibernate queries, JPA entity mappings, and so forth.
The Spring Framework provides first-class support for integration testing in the spring-test
module. The name of the actual JAR file might include the release version and might also be in the long org.springframework.test
form, depending on where you get it from (see the section on Dependency Management for an explanation). This library includes the org.springframework.test
package, which contains valuable classes for integration testing with a Spring container. This testing does not rely on an application server or other deployment environment. Such tests are slower to run than unit tests but much faster than the equivalent Selenium tests or remote tests that rely on deployment to an application server.
In Spring 2.5 and later, unit and integration testing support is provided in the form of the annotation-driven Spring TestContext Framework. The TestContext framework is agnostic of the actual testing framework in use, which allows instrumentation of tests in various environments, including JUnit, TestNG, and others.
3.2. Goals of Integration Testing
Spring’s integration testing support has the following primary goals:
-
To manage Spring IoC container caching between tests.
-
To provide Dependency Injection of test fixture instances.
-
To provide transaction management appropriate to integration testing.
-
To supply Spring-specific base classes that assist developers in writing integration tests.
The next few sections describe each goal and provide links to implementation and configuration details.
3.2.1. Context Management and Caching
The Spring TestContext Framework provides consistent loading of Spring ApplicationContext
instances and WebApplicationContext
instances as well as caching of those contexts. Support for the caching of loaded contexts is important, because startup time can become an issue — not because of the overhead of Spring itself, but because the objects instantiated by the Spring container take time to instantiate. For example, a project with 50 to 100 Hibernate mapping files might take 10 to 20 seconds to load the mapping files, and incurring that cost before running every test in every test fixture leads to slower overall test runs that reduce developer productivity.
Test classes typically declare either an array of resource locations for XML or Groovy configuration metadata — often in the classpath — or an array of annotated classes that is used to configure the application. These locations or classes are the same as or similar to those specified in web.xml
or other configuration files for production deployments.
By default, once loaded, the configured ApplicationContext
is reused for each test. Thus, the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term “test suite” means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module. In the unlikely case that a test corrupts the application context and requires reloading (for example, by modifying a bean definition or the state of an application object) the TestContext framework can be configured to reload the configuration and rebuild the application context before executing the next test.
See Context Management and Context Caching with the TestContext framework.
3.2.2. Dependency Injection of Test Fixtures
When the TestContext framework loads your application context, it can optionally configure instances of your test classes by using Dependency Injection. This provides a convenient mechanism for setting up test fixtures by using preconfigured beans from your application context. A strong benefit here is that you can reuse application contexts across various testing scenarios (for example, for configuring Spring-managed object graphs, transactional proxies, DataSource
instances, and others), thus avoiding the need to duplicate complex test fixture setup for individual test cases.
As an example, consider a scenario where we have a class (HibernateTitleRepository
) that implements data access logic for a Title
domain entity. We want to write integration tests that test the following areas:
-
The Spring configuration: Basically, is everything related to the configuration of the
HibernateTitleRepository
bean correct and present? -
The Hibernate mapping file configuration: Is everything mapped correctly and are the correct lazy-loading settings in place?
-
The logic of the
HibernateTitleRepository
: Does the configured instance of this class perform as anticipated?
See dependency injection of test fixtures with the TestContext framework.
3.2.3. Transaction Management
One common issue in tests that access a real database is their effect on the state of the persistence store. Even when you use a development database, changes to the state may affect future tests. Also, many operations — such as inserting or modifying persistent data — cannot be performed (or verified) outside of a transaction.
The TestContext framework addresses this issue. By default, the framework creates and rolls back a transaction for each test. You can write code that can assume the existence of a transaction. If you call transactionally proxied objects in your tests, they behave correctly, according to their configured transactional semantics. In addition, if a test method deletes the contents of selected tables while running within the transaction managed for the test, the transaction rolls back by default, and the database returns to its state prior to execution of the test. Transactional support is provided to a test by using a PlatformTransactionManager
bean defined in the test’s application context.
If you want a transaction to commit (unusual, but occasionally useful when you want a particular test to populate or modify the database), you can tell the TestContext framework to cause the transaction to commit instead of roll back by using the @Commit
annotation.
See transaction management with the TestContext framework.
3.2.4. Support Classes for Integration Testing
The Spring TestContext Framework provides several abstract
support classes that simplify the writing of integration tests. These base test classes provide well-defined hooks into the testing framework as well as convenient instance variables and methods, which let you access:
-
The
ApplicationContext
, for performing explicit bean lookups or testing the state of the context as a whole. -
A
JdbcTemplate
, for executing SQL statements to query the database. You can use such queries to confirm database state both before and after execution of database-related application code, and Spring ensures that such queries run in the scope of the same transaction as the application code. When used in conjunction with an ORM tool, be sure to avoid false positives.
In addition, you may want to create your own custom, application-wide superclass with instance variables and methods specific to your project.
See support classes for the TestContext framework.
3.3. JDBC Testing Support
The org.springframework.test.jdbc
package contains JdbcTestUtils
, which is a collection of JDBC-related utility functions intended to simplify standard database testing scenarios. Specifically, JdbcTestUtils
provides the following static utility methods.
-
countRowsInTable(..)
: Counts the number of rows in the given table. -
countRowsInTableWhere(..)
: Counts the number of rows in the given table by using the providedWHERE
clause. -
deleteFromTables(..)
: Deletes all rows from the specified tables. -
deleteFromTableWhere(..)
: Deletes rows from the given table by using the providedWHERE
clause. -
dropTables(..)
: Drops the specified tables.
The |
3.4. Annotations
This section covers annotations that you can use when you test Spring applications. It includes the following topics:
3.4.1. Spring Testing Annotations
The Spring Framework provides the following set of Spring-specific annotations that you can use in your unit and integration tests in conjunction with the TestContext framework. See the corresponding javadoc for further information, including default attribute values, attribute aliases, and other details.
Spring’s testing annotations include the following:
@BootstrapWith
@BootstrapWith
is a class-level annotation that you can use to configure how the Spring TestContext Framework is bootstrapped. Specifically, you can use @BootstrapWith
to specify a custom TestContextBootstrapper
. See the Bootstrapping the TestContext framework section for further details.
@ContextConfiguration
@ContextConfiguration
defines class-level metadata that is used to determine how to load and configure an ApplicationContext
for integration tests. Specifically, @ContextConfiguration
declares the application context resource locations
or the annotated classes
used to load the context.
Resource locations are typically XML configuration files or Groovy scripts located in the classpath, while annotated classes are typically @Configuration
classes. However, resource locations can also refer to files and scripts in the file system, and annotated classes can be component classes, and so on.
The following example shows a @ContextConfiguration
annotation that refers to an XML file:
@ContextConfiguration("/test-config.xml") (1)
public class XmlApplicationContextTests {
// class body...
}
1 | Referring to an XML file. |
The following example shows a @ContextConfiguration
annotation that refers to a class:
@ContextConfiguration(classes = TestConfig.class) (1)
public class ConfigClassApplicationContextTests {
// class body...
}
1 | Referring to a class. |
As an alternative or in addition to declaring resource locations or annotated classes, you can use @ContextConfiguration
to declare ApplicationContextInitializer
classes. The following example shows such a case:
@ContextConfiguration(initializers = CustomContextIntializer.class) (1)
public class ContextInitializerTests {
// class body...
}
1 | Declaring an initializer class. |
You can optionally use @ContextConfiguration
to declare the ContextLoader
strategy as well. Note, however, that you typically do not need to explicitly configure the loader, since the default loader supports initializers
and either resource locations
or annotated classes
.
The following example uses both a location and a loader:
@ContextConfiguration(locations = "/test-context.xml", loader = CustomContextLoader.class) (1)
public class CustomLoaderXmlApplicationContextTests {
// class body...
}
1 | Configuring both a location and a custom loader. |
@ContextConfiguration provides support for inheriting resource locations or configuration classes as well as context initializers that are declared by superclasses. |
See Context Management and the @ContextConfiguration
javadocs for further details.
@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:
@ContextConfiguration
@WebAppConfiguration (1)
public 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:
@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") (1)
public 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.
@ContextHierarchy
@ContextHierarchy
is a class-level annotation that is used to define a hierarchy of ApplicationContext
instances for integration tests. @ContextHierarchy
should be declared with a list of one or more @ContextConfiguration
instances, each of which defines a level in the context hierarchy. The following examples demonstrate the use of @ContextHierarchy
within a single test class (@ContextHierarchy
can also be used within a test class hierarchy):
@ContextHierarchy({
@ContextConfiguration("/parent-config.xml"),
@ContextConfiguration("/child-config.xml")
})
public class ContextHierarchyTests {
// class body...
}
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(classes = AppConfig.class),
@ContextConfiguration(classes = WebConfig.class)
})
public class WebIntegrationTests {
// class body...
}
If you need to merge or override the configuration for a given level of the context hierarchy within a test class hierarchy, you must explicitly name that level by supplying the same value to the name
attribute in @ContextConfiguration
at each corresponding level in the class hierarchy. See Context Hierarchies and the @ContextHierarchy
javadoc for further examples.
@ActiveProfiles
@ActiveProfiles
is a class-level annotation that is used to declare which bean definition profiles should be active when loading an ApplicationContext
for an integration test.
The following example indicates that the dev
profile should be active:
@ContextConfiguration
@ActiveProfiles("dev") (1)
public class DeveloperTests {
// class body...
}
1 | Indicate that the dev profile should be active. |
The following example indicates that both the dev
and the integration
profiles should be active:
@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
public class DeveloperIntegrationTests {
// class body...
}
1 | Indicate that the dev and integration profiles should be active. |
@ActiveProfiles provides support for inheriting active bean definition profiles declared by superclasses by default. You can also resolve active bean definition profiles programmatically by implementing a custom ActiveProfilesResolver and registering it by using the resolver attribute of @ActiveProfiles . |
See Context Configuration with Environment Profiles and the @ActiveProfiles
javadoc for examples and further details.
@TestPropertySource
@TestPropertySource
is a class-level annotation that you can use to configure the locations of properties files and inlined properties to be added to the set of PropertySources
in the Environment
for an ApplicationContext
loaded for an integration test.
Test property sources have higher precedence than those loaded from the operating system’s environment or Java system properties as well as property sources added by the application declaratively through @PropertySource
or programmatically. Thus, test property sources can be used to selectively override properties defined in system and application property sources. Furthermore, inlined properties have higher precedence than properties loaded from resource locations.
The following example demonstrates how to declare a properties file from the classpath:
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
public class MyIntegrationTests {
// class body...
}
1 | Get properties from test.properties in the root of the classpath. |
The following example demonstrates how to declare inlined properties:
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) (1)
public class MyIntegrationTests {
// class body...
}
1 | Declare timezone and port properties. |
@DirtiesContext
@DirtiesContext
indicates that the underlying Spring ApplicationContext
has been dirtied during the execution of a test (that is, the test modified or corrupted it in some manner — for example, by changing the state of a singleton bean) and should be closed. When an application context is marked as dirty, it is removed from the testing framework’s cache and closed. As a consequence, the underlying Spring container is rebuilt for any subsequent test that requires a context with the same configuration metadata.
You can use @DirtiesContext
as both a class-level and a method-level annotation within the same class or class hierarchy. In such scenarios, the ApplicationContext
is marked as dirty before or after any such annotated method as well as before or after the current test class, depending on the configured methodMode
and classMode
.
The following examples explain when the context would be dirtied for various configuration scenarios:
-
Before the current test class, when declared on a class with class mode set to
BEFORE_CLASS
.@DirtiesContext(classMode = BEFORE_CLASS) (1) public class FreshContextTests { // some tests that require a new Spring container }
1 Dirty the context before the current test class. -
After the current test class, when declared on a class with class mode set to
AFTER_CLASS
(i.e., the default class mode).@DirtiesContext (1) public class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 Dirty the context after the current test class. -
Before each test method in the current test class, when declared on a class with class mode set to
BEFORE_EACH_TEST_METHOD.
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) public class FreshContextTests { // some tests that require a new Spring container }
1 Dirty the context before each test method. -
After each test method in the current test class, when declared on a class with class mode set to
AFTER_EACH_TEST_METHOD.
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) public class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 Dirty the context after each test method. -
Before the current test, when declared on a method with the method mode set to
BEFORE_METHOD
.@DirtiesContext(methodMode = BEFORE_METHOD) (1) @Test public void testProcessWhichRequiresFreshAppCtx() { // some logic that requires a new Spring container }
1 Dirty the context before the current test method. -
After the current test, when declared on a method with the method mode set to
AFTER_METHOD
(i.e., the default method mode).@DirtiesContext (1) @Test public void testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied }
1 Dirty the context after the current test method.
If you use @DirtiesContext
in a test whose context is configured as part of a context hierarchy with @ContextHierarchy
, you can use the hierarchyMode
flag to control how the context cache is cleared. By default, an exhaustive algorithm is used to clear the context cache, including not only the current level but also all other context hierarchies that share an ancestor context common to the current test. All ApplicationContext
instances that reside in a sub-hierarchy of the common ancestor context are removed from the context cache and closed. If the exhaustive algorithm is overkill for a particular use case, you can specify the simpler current level algorithm, as the following example shows.
@ContextHierarchy({
@ContextConfiguration("/parent-config.xml"),
@ContextConfiguration("/child-config.xml")
})
public class BaseTests {
// class body...
}
public class ExtendedTests extends BaseTests {
@Test
@DirtiesContext(hierarchyMode = CURRENT_LEVEL) (1)
public void test() {
// some logic that results in the child context being dirtied
}
}
1 | Use the current-level algorithm. |
For further details regarding the EXHAUSTIVE
and CURRENT_LEVEL
algorithms, see the DirtiesContext.HierarchyMode
javadoc.
@TestExecutionListeners
@TestExecutionListeners
defines class-level metadata for configuring the TestExecutionListener
implementations that should be registered with the TestContextManager
. Typically, @TestExecutionListeners
is used in conjunction with @ContextConfiguration
.
The following example shows how to register two TestExecutionListener
implementations:
@ContextConfiguration
@TestExecutionListeners({CustomTestExecutionListener.class, AnotherTestExecutionListener.class}) (1)
public class CustomTestExecutionListenerTests {
// class body...
}
1 | Register two TestExecutionListener implementations. |
By default, @TestExecutionListeners
supports inherited listeners. See the javadoc for an example and further details.
@Commit
@Commit
indicates that the transaction for a transactional test method should be committed after the test method has completed. You can use @Commit
as a direct replacement for @Rollback(false)
to more explicitly convey the intent of the code. Analogous to @Rollback
, @Commit
can also be declared as a class-level or method-level annotation.
The following example shows how to use the @Commit
annotation:
@Commit (1)
@Test
public void testProcessWithoutRollback() {
// ...
}
1 | Commit the result of the test to the database. |
@Rollback
@Rollback
indicates whether the transaction for a transactional test method should be rolled back after the test method has completed. If true
, the transaction is rolled back. Otherwise, the transaction is committed (see also @Commit
). Rollback for integration tests in the Spring TestContext Framework defaults to true
even if @Rollback
is not explicitly declared.
When declared as a class-level annotation, @Rollback
defines the default rollback semantics for all test methods within the test class hierarchy. When declared as a method-level annotation, @Rollback
defines rollback semantics for the specific test method, potentially overriding class-level @Rollback
or @Commit
semantics.
The following example causes a test method’s result to not be rolled back (that is, the result is committed to the database):
@Rollback(false) (1)
@Test
public void testProcessWithoutRollback() {
// ...
}
1 | Do not roll back the result. |
@BeforeTransaction
@BeforeTransaction
indicates that the annotated void
method should be run before a transaction is started, for test methods that have been configured to run within a transaction by using Spring’s @Transactional
annotation. As of Spring Framework 4.3, @BeforeTransaction
methods are not required to be public
and may be declared on Java 8-based interface default methods.
The following example shows how to use the @BeforeTransaction
annotation:
@BeforeTransaction (1)
void beforeTransaction() {
// logic to be executed before a transaction is started
}
1 | Run this method before a transaction. |
@AfterTransaction
@AfterTransaction
indicates that the annotated void
method should be run after a transaction is ended, for test methods that have been configured to run within a transaction by using Spring’s @Transactional
annotation. As of Spring Framework 4.3, @AfterTransaction
methods are not required to be public
and may be declared on Java 8-based interface default methods.
@AfterTransaction (1)
void afterTransaction() {
// logic to be executed after a transaction has ended
}
1 | Run this method after a transaction. |
@Sql
@Sql
is used to annotate a test class or test method to configure SQL scripts to be run against a given database during integration tests. The following example shows how to use it:
@Test
@Sql({"/test-schema.sql", "/test-user-data.sql"}) (1)
public void userTest {
// execute code that relies on the test schema and test data
}
1 | Run two scripts for this test. |
See Executing SQL scripts declaratively with @Sql for further details.
@SqlConfig
@SqlConfig
defines metadata that is used to determine how to parse and run SQL scripts configured with the @Sql
annotation. The following example shows how to use it:
@Test
@Sql(
scripts = "/test-user-data.sql",
config = @SqlConfig(commentPrefix = "`", separator = "@@") (1)
)
public void userTest {
// execute code that relies on the test data
}
1 | Set the comment prefix and the separator in SQL scripts. |
@SqlGroup
@SqlGroup
is a container annotation that aggregates several @Sql
annotations. You can use @SqlGroup
natively to declare several nested @Sql
annotations, or you can use it in conjunction with Java 8’s support for repeatable annotations, where @Sql
can be declared several times on the same class or method, implicitly generating this container annotation. The following example shows how to declare an SQL group:
@Test
@SqlGroup({ (1)
@Sql(scripts = "/test-schema.sql", config = @SqlConfig(commentPrefix = "`")),
@Sql("/test-user-data.sql")
)}
public void userTest {
// execute code that uses the test schema and test data
}
1 | Declare a group of SQL scripts. |
3.4.2. Standard Annotation Support
The following annotations are supported with standard semantics for all configurations of the Spring TestContext Framework. Note that these annotations are not specific to tests and can be used anywhere in the Spring Framework.
-
@Autowired
-
@Qualifier
-
@Resource
(javax.annotation) if JSR-250 is present -
@ManagedBean
(javax.annotation) if JSR-250 is present -
@Inject
(javax.inject) if JSR-330 is present -
@Named
(javax.inject) if JSR-330 is present -
@PersistenceContext
(javax.persistence) if JPA is present -
@PersistenceUnit
(javax.persistence) if JPA is present -
@Required
-
@Transactional
JSR-250 Lifecycle Annotations
In the Spring TestContext Framework, you can use If a method within a test class is annotated with |
3.4.3. Spring JUnit 4 Testing Annotations
The following annotations are supported only when used in conjunction with the SpringRunner, Spring’s JUnit 4 rules, or Spring’s JUnit 4 support classes:
@IfProfileValue
@IfProfileValue
indicates that the annotated test is enabled for a specific testing environment. If the configured ProfileValueSource
returns a matching value
for the provided name
, the test is enabled. Otherwise, the test is disabled and, effectively, ignored.
You can apply @IfProfileValue
at the class level, the method level, or both. Class-level usage of @IfProfileValue
takes precedence over method-level usage for any methods within that class or its subclasses. Specifically, a test is enabled if it is enabled both at the class level and at the method level. The absence of @IfProfileValue
means the test is implicitly enabled. This is analogous to the semantics of JUnit 4’s @Ignore
annotation, except that the presence of @Ignore
always disables a test.
The following example shows a test that has an @IfProfileValue
annotation:
@IfProfileValue(name="java.vendor", value="Oracle Corporation") (1)
@Test
public void testProcessWhichRunsOnlyOnOracleJvm() {
// some logic that should run only on Java VMs from Oracle Corporation
}
1 | Run this test only when the Java vendor is "Oracle Corporation". |
Alternatively, you can configure @IfProfileValue
with a list of values
(with OR
semantics) to achieve TestNG-like support for test groups in a JUnit 4 environment. Consider the following example:
@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) (1)
@Test
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
1 | Run this test for unit tests and integration tests. |
@ProfileValueSourceConfiguration
@ProfileValueSourceConfiguration
is a class-level annotation that specifies what type of ProfileValueSource
to use when retrieving profile values configured through the @IfProfileValue
annotation. If @ProfileValueSourceConfiguration
is not declared for a test, SystemProfileValueSource
is used by default. The following example shows how to use @ProfileValueSourceConfiguration
:
@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
// class body...
}
1 | Use a custom profile value source. |
@Timed
@Timed
indicates that the annotated test method must finish execution in a specified time period (in milliseconds). If the text execution time exceeds the specified time period, the test fails.
The time period includes running the test method itself, any repetitions of the test (see @Repeat
), as well as any setting up or tearing down of the test fixture. The following example shows how to use it:
@Timed(millis = 1000) (1)
public void testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to execute
}
1 | Set the time period for the test to one second. |
Spring’s @Timed
annotation has different semantics than JUnit 4’s @Test(timeout=…)
support. Specifically, due to the manner in which JUnit 4 handles test execution timeouts (that is, by executing the test method in a separate Thread
), @Test(timeout=…)
preemptively fails the test if the test takes too long. Spring’s @Timed
, on the other hand, does not preemptively fail the test but rather waits for the test to complete before failing.
@Repeat
@Repeat
indicates that the annotated test method must be run repeatedly. The number of times that the test method is to be executed is specified in the annotation.
The scope of execution to be repeated includes execution of the test method itself as well as any setting up or tearing down of the test fixture. The following example shows how to use the @Repeat
annotation:
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
1 | Repeat this test ten times. |
3.4.4. Spring JUnit Jupiter Testing Annotations
The following annotations are supported only when used in conjunction with the SpringExtension
and JUnit Jupiter (that is, the programming model in JUnit 5):
@SpringJUnitConfig
@SpringJUnitConfig
is a composed annotation that combines @ExtendWith(SpringExtension.class)
from JUnit Jupiter with @ContextConfiguration
from the Spring TestContext Framework. It can be used at the class level as a drop-in replacement for @ContextConfiguration
. With regard to configuration options, the only difference between @ContextConfiguration
and @SpringJUnitConfig
is that annotated classes may be declared with the value
attribute in @SpringJUnitConfig
.
The following example shows how to use the @SpringJUnitConfig
annotation to specify a configuration class:
@SpringJUnitConfig(TestConfig.class) (1)
class ConfigurationClassJUnitJupiterSpringTests {
// class body...
}
1 | Specify the configuration class. |
The following example shows how to use the @SpringJUnitConfig
annotation to specify the location of a configuration file:
@SpringJUnitConfig(locations = "/test-config.xml") (1)
class XmlJUnitJupiterSpringTests {
// class body...
}
1 | Specify the location of a configuration file. |
See Context Management as well as the javadoc for @SpringJUnitConfig
and @ContextConfiguration
for further details.
@SpringJUnitWebConfig
@SpringJUnitWebConfig
is a composed annotation that combines @ExtendWith(SpringExtension.class)
from JUnit Jupiter with @ContextConfiguration
and @WebAppConfiguration
from the Spring TestContext Framework. You can use it at the class level as a drop-in replacement for @ContextConfiguration
and @WebAppConfiguration
. With regard to configuration options, the only difference between @ContextConfiguration
and @SpringJUnitWebConfig
is that you can declare annotated classes bu using the value
attribute in @SpringJUnitWebConfig
. In addition, you can override the value
attribute from @WebAppConfiguration
only by using the resourcePath
attribute in @SpringJUnitWebConfig
.
The following example shows how to use the @SpringJUnitWebConfig
annotation to specify a configuration class:
@SpringJUnitWebConfig(TestConfig.class) (1)
class ConfigurationClassJUnitJupiterSpringWebTests {
// class body...
}
1 | Specify the configuration class. |
The following example shows how to use the @SpringJUnitWebConfig
annotation to specify a the location of a configuration file:
@SpringJUnitWebConfig(locations = "/test-config.xml") (1)
class XmlJUnitJupiterSpringWebTests {
// class body...
}
1 | Specify the location of a configuration file. |
See Context Management as well as the javadoc for @SpringJUnitWebConfig
, @ContextConfiguration
, and @WebAppConfiguration
for further details.
@EnabledIf
@EnabledIf
is used to signal that the annotated JUnit Jupiter test class or test method is enabled and should be run if the supplied expression
evaluates to true
. Specifically, if the expression evaluates to Boolean.TRUE
or a String
equal to true
(ignoring case), the test is enabled. When applied at the class level, all test methods within that class are automatically enabled by default as well.
Expressions can be any of the following:
-
Spring Expression Language (SpEL) expression. For example:
@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
-
Placeholder for a property available in the Spring
Environment
. For example:@EnabledIf("${smoke.tests.enabled}")
-
Text literal. For example:
@EnabledIf("true")
Note, however, that a text literal that is not the result of dynamic resolution of a property placeholder is of zero practical value, since @EnabledIf("false")
is equivalent to @Disabled
and @EnabledIf("true")
is logically meaningless.
You can use @EnabledIf
as a meta-annotation to create custom composed annotations. For example, you can create a custom @EnabledOnMac
annotation as follows:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@EnabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Enabled on Mac OS"
)
public @interface EnabledOnMac {}
@DisabledIf
@DisabledIf
is used to signal that the annotated JUnit Jupiter test class or test method is disabled and should not be executed if the supplied expression
evaluates to true
. Specifically, if the expression evaluates to Boolean.TRUE
or a String
equal to true
(ignoring case), the test is disabled. When applied at the class level, all test methods within that class are automatically disabled as well.
Expressions can be any of the following:
-
Spring Expression Language (SpEL) expression. For example:
@DisabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
-
Placeholder for a property available in the Spring
Environment
. For example:@DisabledIf("${smoke.tests.disabled}")
-
Text literal. For example:
@DisabledIf("true")
Note, however, that a text literal that is not the result of dynamic resolution of a property placeholder is of zero practical value, since @DisabledIf("true")
is equivalent to @Disabled
and @DisabledIf("false")
is logically meaningless.
You can use @DisabledIf
as a meta-annotation to create custom composed annotations. For example, you can create a custom @DisabledOnMac
annotation as follows:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@DisabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Disabled on Mac OS"
)
public @interface DisabledOnMac {}
3.4.5. Meta-Annotation Support for Testing
You can use most test-related annotations as meta-annotations to create custom composed annotations and reduce configuration duplication across a test suite.
You can use each of the following as a meta-annotation in conjunction with the TestContext framework.
-
@BootstrapWith
-
@ContextConfiguration
-
@ContextHierarchy
-
@ActiveProfiles
-
@TestPropertySource
-
@DirtiesContext
-
@WebAppConfiguration
-
@TestExecutionListeners
-
@Transactional
-
@BeforeTransaction
-
@AfterTransaction
-
@Commit
-
@Rollback
-
@Sql
-
@SqlConfig
-
@SqlGroup
-
@Repeat
(only supported on JUnit 4) -
@Timed
(only supported on JUnit 4) -
@IfProfileValue
(only supported on JUnit 4) -
@ProfileValueSourceConfiguration
(only supported on JUnit 4) -
@SpringJUnitConfig
(only supported on JUnit Jupiter) -
@SpringJUnitWebConfig
(only supported on JUnit Jupiter) -
@EnabledIf
(only supported on JUnit Jupiter) -
@DisabledIf
(only supported on JUnit Jupiter)
Consider the following example:
@RunWith(SpringRunner.class)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
public class OrderRepositoryTests { }
@RunWith(SpringRunner.class)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
public class UserRepositoryTests { }
If we discover that we are repeating the preceding configuration across our JUnit 4-based test suite, we can reduce the duplication by introducing a custom composed annotation that centralizes the common test configuration for Spring, as follows:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
public @interface TransactionalDevTestConfig { }
Then we can use our custom @TransactionalDevTestConfig
annotation to simplify the configuration of individual JUnit 4 based test classes, as follows:
@RunWith(SpringRunner.class)
@TransactionalDevTestConfig
public class OrderRepositoryTests { }
@RunWith(SpringRunner.class)
@TransactionalDevTestConfig
public class UserRepositoryTests { }
If we write tests that use JUnit Jupiter, we can reduce code duplication even further, since annotations in JUnit 5 can also be used as meta-annotations. Consider the following example:
@ExtendWith(SpringExtension.class)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
class OrderRepositoryTests { }
@ExtendWith(SpringExtension.class)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
class UserRepositoryTests { }
If we discover that we are repeating the preceding configuration across our JUnit Jupiter-based test suite, we can reduce the duplication by introducing a custom composed annotation that centralizes the common test configuration for Spring and JUnit Jupiter, as follows:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(SpringExtension.class)
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
@ActiveProfiles("dev")
@Transactional
public @interface TransactionalDevTestConfig { }
Then we can use our custom @TransactionalDevTestConfig
annotation to simplify the configuration of individual JUnit Jupiter based test classes, as follows:
@TransactionalDevTestConfig
class OrderRepositoryTests { }
@TransactionalDevTestConfig
class UserRepositoryTests { }
Since JUnit Jupiter supports the use of @Test
, @RepeatedTest
, ParameterizedTest
, and others as meta-annotations, you can also create custom composed annotations at the test method level. For example, if we wish to create a composed annotation that combines the @Test
and @Tag
annotations from JUnit Jupiter with the @Transactional
annotation from Spring, we could create an @TransactionalIntegrationTest
annotation, as follows:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Transactional
@Tag("integration-test") // org.junit.jupiter.api.Tag
@Test // org.junit.jupiter.api.Test
public @interface TransactionalIntegrationTest { }
Then we can use our custom @TransactionalIntegrationTest
annotation to simplify the configuration of individual JUnit Jupiter based test methods, as follows:
@TransactionalIntegrationTest
void saveOrder() { }
@TransactionalIntegrationTest
void deleteOrder() { }
For further details, see the Spring Annotation Programming Model wiki page.
3.5. Spring TestContext Framework
The Spring TestContext Framework (located in the org.springframework.test.context
package) provides generic, annotation-driven unit and integration testing support that is agnostic of the testing framework in use. The TestContext framework also places a great deal of importance on convention over configuration, with reasonable defaults that you can override through annotation-based configuration.
In addition to generic testing infrastructure, the TestContext framework provides explicit support for JUnit 4, JUnit Jupiter (AKA JUnit 5), and TestNG. For JUnit 4 and TestNG, Spring provides abstract
support classes. Furthermore, Spring provides a custom JUnit Runner
and custom JUnit Rules
for JUnit 4 and a custom Extension
for JUnit Jupiter that let you write so-called POJO test classes. POJO test classes are not required to extend a particular class hierarchy, such as the abstract
support classes.
The following section provides an overview of the internals of the TestContext framework. If you are interested only in using the framework and are not interested in extending it with your own custom listeners or custom loaders, feel free to go directly to the configuration (context management, dependency injection, transaction management), support classes, and annotation support sections.
3.5.1. Key Abstractions
The core of the framework consists of the TestContextManager
class and the TestContext
, TestExecutionListener
, and SmartContextLoader
interfaces. A TestContextManager
is created for each test class (for example, for the execution of all test methods within a single test class in JUnit Jupiter). The TestContextManager
, in turn, manages a TestContext
that holds the context of the current test. The TestContextManager
also updates the state of the TestContext
as the test progresses and delegates to TestExecutionListener
implementations, which instrument the actual test execution by providing dependency injection, managing transactions, and so on. A SmartContextLoader
is responsible for loading an ApplicationContext
for a given test class. See the javadoc and the Spring test suite for further information and examples of various implementations.
TestContext
TestContext
encapsulates the context in which a test is executed (agnostic of the actual testing framework in use) and provides context management and caching support for the test instance for which it is responsible. The TestContext
also delegates to a SmartContextLoader
to load an ApplicationContext
if requested.
TestContextManager
TestContextManager
is the main entry point into the Spring TestContext Framework and is responsible for managing a single TestContext
and signaling events to each registered TestExecutionListener
at well-defined test execution points:
-
Prior to any “before class” or “before all” methods of a particular testing framework.
-
Test instance post-processing.
-
Prior to any “before” or “before each” methods of a particular testing framework.
-
Immediately before execution of the test method but after test setup.
-
Immediately after execution of the test method but before test tear down.
-
After any “after” or “after each” methods of a particular testing framework.
-
After any “after class” or “after all” methods of a particular testing framework.
TestExecutionListener
TestExecutionListener
defines the API for reacting to test-execution events published by the TestContextManager
with which the listener is registered. See TestExecutionListener
Configuration.
Context Loaders
ContextLoader
is a strategy interface that was introduced in Spring 2.5 for loading an ApplicationContext
for an integration test managed by the Spring TestContext Framework. You should implement SmartContextLoader
instead of this interface to provide support for annotated classes, active bean definition profiles, test property sources, context hierarchies, and WebApplicationContext
support.
SmartContextLoader
is an extension of the ContextLoader
interface introduced in Spring 3.1. The SmartContextLoader
SPI supersedes the ContextLoader
SPI that was introduced in Spring 2.5. Specifically, a SmartContextLoader
can choose to process resource locations, annotated classes, or context initializers. Furthermore, a SmartContextLoader
can set active bean definition profiles and test property sources in the context that it loads.
Spring provides the following implementations:
-
DelegatingSmartContextLoader
: One of two default loaders, it delegates internally to anAnnotationConfigContextLoader
, aGenericXmlContextLoader
, or aGenericGroovyXmlContextLoader
, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. Groovy support is enabled only if Groovy is on the classpath. -
WebDelegatingSmartContextLoader
: One of two default loaders, it delegates internally to anAnnotationConfigWebContextLoader
, aGenericXmlWebContextLoader
, or aGenericGroovyXmlWebContextLoader
, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. A webContextLoader
is used only if@WebAppConfiguration
is present on the test class. Groovy support is enabled only if Groovy is on the classpath. -
AnnotationConfigContextLoader
: Loads a standardApplicationContext
from annotated classes. -
AnnotationConfigWebContextLoader
: Loads aWebApplicationContext
from annotated classes. -
GenericGroovyXmlContextLoader
: Loads a standardApplicationContext
from resource locations that are either Groovy scripts or XML configuration files. -
GenericGroovyXmlWebContextLoader
: Loads aWebApplicationContext
from resource locations that are either Groovy scripts or XML configuration files. -
GenericXmlContextLoader
: Loads a standardApplicationContext
from XML resource locations. -
GenericXmlWebContextLoader
: Loads aWebApplicationContext
from XML resource locations. -
GenericPropertiesContextLoader
: Loads a standardApplicationContext
from Java properties files.
3.5.2. Bootstrapping the TestContext Framework
The default configuration for the internals of the Spring TestContext Framework is sufficient for all common use cases. However, there are times when a development team or third party framework would like to change the default ContextLoader
, implement a custom TestContext
or ContextCache
, augment the default sets of ContextCustomizerFactory
and TestExecutionListener
implementations, and so on. For such low-level control over how the TestContext framework operates, Spring provides a bootstrapping strategy.
TestContextBootstrapper
defines the SPI for bootstrapping the TestContext framework. A TestContextBootstrapper
is used by the TestContextManager
to load the TestExecutionListener
implementations for the current test and to build the TestContext
that it manages. You can configure a custom bootstrapping strategy for a test class (or test class hierarchy) by using @BootstrapWith
, either directly or as a meta-annotation. If a bootstrapper is not explicitly configured by using @BootstrapWith
, either the DefaultTestContextBootstrapper
or the WebTestContextBootstrapper
is used, depending on the presence of @WebAppConfiguration
.
Since the TestContextBootstrapper
SPI is likely to change in the future (to accommodate new requirements), we strongly encourage implementers not to implement this interface directly but rather to extend AbstractTestContextBootstrapper
or one of its concrete subclasses instead.
3.5.3. TestExecutionListener
Configuration
Spring provides the following TestExecutionListener
implementations that are registered by default, exactly in the following order:
-
ServletTestExecutionListener
: Configures Servlet API mocks for aWebApplicationContext
. -
DirtiesContextBeforeModesTestExecutionListener
: Handles the@DirtiesContext
annotation for “before” modes. -
DependencyInjectionTestExecutionListener
: Provides dependency injection for the test instance. -
DirtiesContextTestExecutionListener
: Handles the@DirtiesContext
annotation for “after” modes. -
TransactionalTestExecutionListener
: Provides transactional test execution with default rollback semantics. -
SqlScriptsTestExecutionListener
: Runs SQL scripts configured by using the@Sql
annotation.
Registering Custom TestExecutionListener
Implementations
You can register custom TestExecutionListener
implementations for a test class and its subclasses by using the @TestExecutionListeners
annotation. See annotation support and the javadoc for @TestExecutionListeners
for details and examples.
Automatic Discovery of Default TestExecutionListener
Implementations
Registering custom TestExecutionListener
implementations by using @TestExecutionListeners
is suitable for custom listeners that are used in limited testing scenarios. However, it can become cumbersome if a custom listener needs to be used across a test suite. Since Spring Framework 4.1, this issue is addressed through support for automatic discovery of default TestExecutionListener
implementations through the SpringFactoriesLoader
mechanism.
Specifically, the spring-test
module declares all core default TestExecutionListener` implementations under the org.springframework.test.context.TestExecutionListener
key in its META-INF/spring.factories
properties file. Third-party frameworks and developers can contribute their own TestExecutionListener
implementations to the list of default listeners in the same manner through their own META-INF/spring.factories
properties file.
Ordering TestExecutionListener
Implementations
When the TestContext framework discovers default TestExecutionListener
implementations through the aforementioned SpringFactoriesLoader
mechanism, the instantiated listeners are sorted by using Spring’s AnnotationAwareOrderComparator
, which honors Spring’s Ordered
interface and @Order
annotation for ordering. AbstractTestExecutionListener
and all default TestExecutionListener
implementations provided by Spring implement Ordered
with appropriate values. Third-party frameworks and developers should therefore make sure that their default TestExecutionListener
implementations are registered in the proper order by implementing Ordered
or declaring @Order
. See the javadoc for the getOrder()
methods of the core default TestExecutionListener
implementations for details on what values are assigned to each core listener.
Merging TestExecutionListener
Implementations
If a custom TestExecutionListener
is registered via @TestExecutionListeners
, the default listeners are not registered. In most common testing scenarios, this effectively forces the developer to manually declare all default listeners in addition to any custom listeners. The following listing demonstrates this style of configuration:
@ContextConfiguration
@TestExecutionListeners({
MyCustomTestExecutionListener.class,
ServletTestExecutionListener.class,
DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class
})
public class MyTest {
// class body...
}
The challenge with this approach is that it requires that the developer know exactly which listeners are registered by default. Moreover, the set of default listeners can change from release to release — for example, SqlScriptsTestExecutionListener
was introduced in Spring Framework 4.1, and DirtiesContextBeforeModesTestExecutionListener
was introduced in Spring Framework 4.2. Furthermore, third-party frameworks like Spring Security register their own default TestExecutionListener
implementations by using the aforementioned automatic discovery mechanism.
To avoid having to be aware of and re-declare all default listeners, you can set the mergeMode
attribute of @TestExecutionListeners
to MergeMode.MERGE_WITH_DEFAULTS
. MERGE_WITH_DEFAULTS
indicates that locally declared listeners should be merged with the default listeners. The merging algorithm ensures that duplicates are removed from the list and that the resulting set of merged listeners is sorted according to the semantics of AnnotationAwareOrderComparator
, as described in Ordering TestExecutionListener
Implementations. If a listener implements Ordered
or is annotated with @Order
, it can influence the position in which it is merged with the defaults. Otherwise, locally declared listeners are appended to the list of default listeners when merged.
For example, if the MyCustomTestExecutionListener
class in the previous example configures its order
value (for example, 500
) to be less than the order of the ServletTestExecutionListener
(which happens to be 1000
), the MyCustomTestExecutionListener
can then be automatically merged with the list of defaults in front of the ServletTestExecutionListener
, and the previous example could be replaced with the following:
@ContextConfiguration
@TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
public class MyTest {
// class body...
}
3.5.4. Context Management
Each TestContext
provides context management and caching support for the test instance for which it is responsible. Test instances do not automatically receive access to the configured ApplicationContext
. However, if a test class implements the ApplicationContextAware
interface, a reference to the ApplicationContext
is supplied to the test instance. Note that AbstractJUnit4SpringContextTests
and AbstractTestNGSpringContextTests
implement ApplicationContextAware
and, therefore, provide access to the ApplicationContext
automatically.
@Autowired ApplicationContext
As an alternative to implementing the
Similarly, if your test is configured to load a
Dependency injection by using |
Test classes that use the TestContext framework do not need to extend any particular class or implement a specific interface to configure their application context. Instead, configuration is achieved by declaring the @ContextConfiguration
annotation at the class level. If your test class does not explicitly declare application context resource locations or annotated classes, the configured ContextLoader
determines how to load a context from a default location or default configuration classes. In addition to context resource locations and annotated classes, an application context can also be configured through application context initializers.
The following sections explain how to use Spring’s @ContextConfiguration
annotation to configure a test ApplicationContext
by using XML configuration files, Groovy scripts, annotated classes (typically @Configuration
classes), or context initializers. Alternatively, you can implement and configure your own custom SmartContextLoader
for advanced use cases.
Context Configuration with XML resources
To load an ApplicationContext
for your tests by using XML configuration files, annotate your test class with @ContextConfiguration
and configure the locations
attribute with an array that contains the resource locations of XML configuration metadata. A plain or relative path (for example, context.xml
) is treated as a classpath resource that is relative to the package in which the test class is defined. A path starting with a slash is treated as an absolute classpath location (for example, /org/example/config.xml
). A path that represents a resource URL (i.e., a path prefixed with classpath:
, file:
, http:
, etc.) is used as is.
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from "/app-config.xml" and
// "/test-config.xml" in the root of the classpath
@ContextConfiguration(locations={"/app-config.xml", "/test-config.xml"}) (1)
public class MyTest {
// class body...
}
1 | Setting the locations attribute to a list of XML files. |
@ContextConfiguration
supports an alias for the locations
attribute through the standard Java value
attribute. Thus, if you do not need to declare additional attributes in @ContextConfiguration
, you can omit the declaration of the locations
attribute name and declare the resource locations by using the shorthand format demonstrated in the following example:
@RunWith(SpringRunner.class)
@ContextConfiguration({"/app-config.xml", "/test-config.xml"}) (1)
public class MyTest {
// class body...
}
1 | Specifying XML files without using the location attribute. |
If you omit both the locations
and the value
attributes from the @ContextConfiguration
annotation, the TestContext framework tries to detect a default XML resource location. Specifically, GenericXmlContextLoader
and GenericXmlWebContextLoader
detect a default location based on the name of the test class. If your class is named com.example.MyTest
, GenericXmlContextLoader
loads your application context from "classpath:com/example/MyTest-context.xml"
. The following example shows how to do so:
package com.example;
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTest-context.xml"
@ContextConfiguration (1)
public class MyTest {
// class body...
}
1 | Loading configuration from the default location. |
Context Configuration with Groovy Scripts
To load an ApplicationContext
for your tests by using Groovy scripts that use the Groovy Bean Definition DSL, you can annotate your test class with @ContextConfiguration
and configure the locations
or value
attribute with an array that contains the resource locations of Groovy scripts. Resource lookup semantics for Groovy scripts are the same as those described for XML configuration files.
Enabling Groovy script support
Support for using Groovy scripts to load an ApplicationContext in the Spring TestContext Framework is enabled automatically if Groovy is on the classpath. |
The following example shows how to specify Groovy configuration files:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from "/AppConfig.groovy" and
// "/TestConfig.groovy" in the root of the classpath
@ContextConfiguration({"/AppConfig.groovy", "/TestConfig.Groovy"}) (1)
public class MyTest {
// class body...
}
1 | Specifying the location of Groovy configuration files. |
If you omit both the locations
and value
attributes from the @ContextConfiguration
annotation, the TestContext framework tries to detect a default Groovy script. Specifically, GenericGroovyXmlContextLoader
and GenericGroovyXmlWebContextLoader
detect a default location based on the name of the test class. If your class is named com.example.MyTest
, the Groovy context loader loads your application context from "classpath:com/example/MyTestContext.groovy"
. The following example shows how to use the default:
package com.example;
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from
// "classpath:com/example/MyTestContext.groovy"
@ContextConfiguration (1)
public class MyTest {
// class body...
}
1 | Loading configuration from the default location. |
Declaring XML configuration and Groovy scripts simultaneously
You can declare both XML configuration files and Groovy scripts simultaneously by using the The following listing shows how to combine both in an integration test:
|
Context Configuration with Annotated Classes
To load an ApplicationContext
for your tests by using annotated classes (see Java-based container configuration), you can annotate your test class with @ContextConfiguration
and configure the classes
attribute with an array that contains references to annotated classes. The following example shows how to do so:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from AppConfig and TestConfig
@ContextConfiguration(classes = {AppConfig.class, TestConfig.class}) (1)
public class MyTest {
// class body...
}
1 | Specifying annotated classes. |
Annotated Classes
The term “annotated class” can refer to any of the following:
See the javadoc of |
If you omit the classes
attribute from the @ContextConfiguration
annotation, the TestContext framework tries to detect the presence of default configuration classes. Specifically, AnnotationConfigContextLoader
and AnnotationConfigWebContextLoader
detect all static
nested classes of the test class that meet the requirements for configuration class implementations, as specified in the @Configuration
javadoc. Note that the name of the configuration class is arbitrary. In addition, a test class can contain more than one static
nested configuration class if desired. In the following example, the OrderServiceTest
class declares a static
nested configuration class named Config
that is automatically used to load the ApplicationContext
for the test class:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from the
// static nested Config class
@ContextConfiguration (1)
public class OrderServiceTest {
@Configuration
static class Config {
// this bean will be injected into the OrderServiceTest class
@Bean
public OrderService orderService() {
OrderService orderService = new OrderServiceImpl();
// set properties, etc.
return orderService;
}
}
@Autowired
private OrderService orderService;
@Test
public void testOrderService() {
// test the orderService
}
}
1 | Loading configuration information from the nested class. |
Mixing XML, Groovy Scripts, and Annotated Classes
It may sometimes be desirable to mix XML configuration files, Groovy scripts, and annotated classes (typically @Configuration
classes) to configure an ApplicationContext
for your tests. For example, if you use XML configuration in production, you may decide that you want to use @Configuration
classes to configure specific Spring-managed components for your tests, or vice versa.
Furthermore, some third-party frameworks (such as Spring Boot) provide first-class support for loading an ApplicationContext
from different types of resources simultaneously (for example, XML configuration files, Groovy scripts, and @Configuration
classes). The Spring Framework, historically, has not supported this for standard deployments. Consequently, most of the SmartContextLoader
implementations that the Spring Framework delivers in the spring-test
module support only one resource type for each test context. However, this does not mean that you cannot use both. One exception to the general rule is that the GenericGroovyXmlContextLoader
and GenericGroovyXmlWebContextLoader
support both XML configuration files and Groovy scripts simultaneously. Furthermore, third-party frameworks may choose to support the declaration of both locations
and classes
through @ContextConfiguration
, and, with the standard testing support in the TestContext framework, you have the following options.
If you want to use resource locations (for example, XML or Groovy) and @Configuration
classes to configure your tests, you must pick one as the entry point, and that one must include or import the other. For example, in XML or Groovy scripts, you can include @Configuration
classes by using component scanning or defining them as normal Spring beans, whereas, in a @Configuration
class, you can use @ImportResource
to import XML configuration files or Groovy scripts. Note that this behavior is semantically equivalent to how you configure your application in production: In production configuration, you define either a set of XML or Groovy resource locations or a set of @Configuration
classes from which your production ApplicationContext
is loaded, but you still have the freedom to include or import the other type of configuration.
Context Configuration with Context Initializers
To configure an ApplicationContext
for your tests by using context initializers, annotate your test class with @ContextConfiguration
and configure the initializers
attribute with an array that contains references to classes that implement ApplicationContextInitializer
. The declared context initializers are then used to initialize the ConfigurableApplicationContext
that is loaded for your tests. Note that the concrete ConfigurableApplicationContext
type supported by each declared initializer must be compatible with the type of ApplicationContext
created by the SmartContextLoader
in use (typically a GenericApplicationContext
). Furthermore, the order in which the initializers are invoked depends on whether they implement Spring’s Ordered
interface or are annotated with Spring’s @Order
annotation or the standard @Priority
annotation. The following example shows how to use initializers:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from TestConfig
// and initialized by TestAppCtxInitializer
@ContextConfiguration(
classes = TestConfig.class,
initializers = TestAppCtxInitializer.class) (1)
public class MyTest {
// class body...
}
1 | Specifying configuration by using a configuration class and an initializer. |
You can also omit the declaration of XML configuration files, Groovy scripts, or annotated classes in @ContextConfiguration
entirely and instead declare only ApplicationContextInitializer
classes, which are then responsible for registering beans in the context — for example, by programmatically loading bean definitions from XML files or configuration classes. The following example shows how to do so:
@RunWith(SpringRunner.class)
// ApplicationContext will be initialized by EntireAppInitializer
// which presumably registers beans in the context
@ContextConfiguration(initializers = EntireAppInitializer.class) (1)
public class MyTest {
// class body...
}
1 | Specifying configuration by using only an initializer. |
Context Configuration Inheritance
@ContextConfiguration
supports boolean inheritLocations
and inheritInitializers
attributes that denote whether resource locations or annotated classes and context initializers declared by superclasses should be inherited. The default value for both flags is true
. This means that a test class inherits the resource locations or annotated classes as well as the context initializers declared by any superclasses. Specifically, the resource locations or annotated classes for a test class are appended to the list of resource locations or annotated classes declared by superclasses. Similarly, the initializers for a given test class are added to the set of initializers defined by test superclasses. Thus, subclasses have the option of extending the resource locations, annotated classes, or context initializers.
If the inheritLocations
or inheritInitializers
attribute in @ContextConfiguration
is set to false
, the resource locations or annotated classes and the context initializers, respectively, for the test class shadow and effectively replace the configuration defined by superclasses.
In the next example, which uses XML resource locations, the ApplicationContext
for ExtendedTest
is loaded from base-config.xml
and extended-config.xml
, in that order. Beans defined in extended-config.xml
can, therefore, override (that is, replace) those defined in base-config.xml
. The following example shows how one class can extend another and use both its own configuration file and the superclass’s configuration file:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from "/base-config.xml"
// in the root of the classpath
@ContextConfiguration("/base-config.xml") (1)
public class BaseTest {
// class body...
}
// ApplicationContext will be loaded from "/base-config.xml" and
// "/extended-config.xml" in the root of the classpath
@ContextConfiguration("/extended-config.xml") (2)
public class ExtendedTest extends BaseTest {
// class body...
}
1 | Configuration file defined in the superclass. |
2 | Configuration file defined in the subclass. |
Similarly, in the next example, which uses annotated classes, the ApplicationContext
for ExtendedTest
is loaded from the BaseConfig
and ExtendedConfig
classes, in that order. Beans defined in ExtendedConfig
can, therefore, override (that is, replace) those defined in BaseConfig
. The following example shows how one class can extend another and use both its own configuration class and the superclass’s configuration class:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from BaseConfig
@ContextConfiguration(classes = BaseConfig.class) (1)
public class BaseTest {
// class body...
}
// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@ContextConfiguration(classes = ExtendedConfig.class) (2)
public class ExtendedTest extends BaseTest {
// class body...
}
1 | Configuration class defined in the superclass. |
2 | Configuration class defined in the subclass. |
In the next example, which uses context initializers, the ApplicationContext
for ExtendedTest
is initialized by using BaseInitializer
and ExtendedInitializer
. Note, however, that the order in which the initializers are invoked depends on whether they implement Spring’s Ordered
interface or are annotated with Spring’s @Order
annotation or the standard @Priority
annotation. The following example shows how one class can extend another and use both its own initializer and the superclass’s initializer:
@RunWith(SpringRunner.class)
// ApplicationContext will be initialized by BaseInitializer
@ContextConfiguration(initializers = BaseInitializer.class) (1)
public class BaseTest {
// class body...
}
// ApplicationContext will be initialized by BaseInitializer
// and ExtendedInitializer
@ContextConfiguration(initializers = ExtendedInitializer.class) (2)
public class ExtendedTest extends BaseTest {
// class body...
}
1 | Initializer defined in the superclass. |
2 | Initializer defined in the subclass. |
Context Configuration with Environment Profiles
Spring 3.1 introduced first-class support in the framework for the notion of environments and profiles (AKA “bean definition profiles”), and integration tests can be configured to activate particular bean definition profiles for various testing scenarios. This is achieved by annotating a test class with the @ActiveProfiles
annotation and supplying a list of profiles that should be activated when loading the ApplicationContext
for the test.
You can use @ActiveProfiles with any implementation of the new SmartContextLoader SPI, but @ActiveProfiles is not supported with implementations of the older ContextLoader SPI. |
Consider two examples with XML configuration and @Configuration
classes: