Class SqlScriptsTestExecutionListener
- All Implemented Interfaces:
Ordered
,AotTestExecutionListener
,TestExecutionListener
TestExecutionListener
that provides support for executing SQL
scripts
and inlined statements
configured via the @Sql
annotation.
Class-level annotations that are constrained to a class-level execution
phase (BEFORE_TEST_CLASS
or
AFTER_TEST_CLASS
) will be run
once before all test methods or
once after all test methods,
respectively. All other scripts and inlined statements will be executed
before or
after execution of the
corresponding test method, depending
on the configured value of the executionPhase
flag.
Scripts and inlined statements will be executed without a transaction,
within an existing Spring-managed transaction, or within an isolated transaction,
depending on the configured value of SqlConfig.transactionMode()
and the
presence of a transaction manager.
Script Resources
For details on default script detection and how script resource locations
are interpreted, see Sql.scripts()
.
Required Spring Beans
A PlatformTransactionManager
and a DataSource
,
just a PlatformTransactionManager
, or just a DataSource
must be defined as beans in the Spring ApplicationContext
for the
corresponding test. Consult the javadocs for SqlConfig.transactionMode()
,
SqlConfig.transactionManager()
, SqlConfig.dataSource()
,
TestContextTransactionUtils.retrieveDataSource(org.springframework.test.context.TestContext, java.lang.String)
, and
TestContextTransactionUtils.retrieveTransactionManager(org.springframework.test.context.TestContext, java.lang.String)
for details
on permissible configuration constellations and on the algorithms used to
locate these beans.
Required Dependencies
Use of this listener requires the spring-jdbc
and spring-tx
modules as well as their transitive dependencies to be present on the classpath.
- Since:
- 4.1
- Author:
- Sam Brannen, Dmitry Semukhin, Andreas Ahlenstorf
- See Also:
-
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterTestClass
(TestContext testContext) Execute SQL scripts configured via@Sql
for the suppliedTestContext
once per test class after all test methods have been run.void
afterTestMethod
(TestContext testContext) Execute SQL scripts configured via@Sql
for the suppliedTestContext
after the current test method.void
beforeTestClass
(TestContext testContext) Execute SQL scripts configured via@Sql
for the suppliedTestContext
once per test class before any test method is run.void
beforeTestMethod
(TestContext testContext) Execute SQL scripts configured via@Sql
for the suppliedTestContext
before the current test method.final int
getOrder()
Returns5000
.void
processAheadOfTime
(RuntimeHints runtimeHints, Class<?> testClass, ClassLoader classLoader) Process the supplied test class and its methods and register run-time hints for any SQL scripts configured or detected as classpath resources via@Sql
.Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener
afterTestExecution, beforeTestExecution, prepareTestInstance
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.test.context.TestExecutionListener
afterTestExecution, beforeTestExecution, prepareTestInstance
-
Constructor Details
-
SqlScriptsTestExecutionListener
public SqlScriptsTestExecutionListener()
-
-
Method Details
-
getOrder
public final int getOrder()Returns5000
.- Specified by:
getOrder
in interfaceOrdered
- Overrides:
getOrder
in classAbstractTestExecutionListener
- Returns:
- the order value
- See Also:
-
beforeTestClass
Execute SQL scripts configured via@Sql
for the suppliedTestContext
once per test class before any test method is run.- Specified by:
beforeTestClass
in interfaceTestExecutionListener
- Overrides:
beforeTestClass
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 6.1
-
afterTestClass
Execute SQL scripts configured via@Sql
for the suppliedTestContext
once per test class after all test methods have been run.- Specified by:
afterTestClass
in interfaceTestExecutionListener
- Overrides:
afterTestClass
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate- Since:
- 6.1
-
beforeTestMethod
Execute SQL scripts configured via@Sql
for the suppliedTestContext
before the current test method.- Specified by:
beforeTestMethod
in interfaceTestExecutionListener
- Overrides:
beforeTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- See Also:
-
afterTestMethod
Execute SQL scripts configured via@Sql
for the suppliedTestContext
after the current test method.- Specified by:
afterTestMethod
in interfaceTestExecutionListener
- Overrides:
afterTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method was executed; nevernull
- See Also:
-
processAheadOfTime
public void processAheadOfTime(RuntimeHints runtimeHints, Class<?> testClass, ClassLoader classLoader) Process the supplied test class and its methods and register run-time hints for any SQL scripts configured or detected as classpath resources via@Sql
.- Specified by:
processAheadOfTime
in interfaceAotTestExecutionListener
- Parameters:
runtimeHints
- theRuntimeHints
to usetestClass
- the test class to processclassLoader
- the classloader to use- Since:
- 6.0
-