@TestExecutionListeners(value=TransactionalTestExecutionListener.class) @Transactional public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests
AbstractTestNGSpringContextTests
which adds convenience functionality
for JDBC access. Expects a DataSource
bean and a
PlatformTransactionManager
bean to be defined in the Spring
application context.
This class exposes a JdbcTemplate
and provides an easy way to
count the number of rows in a table
(potentially with a WHERE clause),
delete from tables,
drop tables, and
execute SQL scripts within a transaction.
Concrete subclasses must fulfill the same requirements outlined in
AbstractTestNGSpringContextTests
.
AbstractTestNGSpringContextTests
,
ContextConfiguration
,
TestExecutionListeners
,
TransactionalTestExecutionListener
,
TransactionConfiguration
,
Transactional
,
NotTransactional
,
Rollback
,
BeforeTransaction
,
AfterTransaction
,
JdbcTestUtils
,
AbstractTransactionalJUnit4SpringContextTests
Modifier and Type | Field and Description |
---|---|
protected JdbcTemplate |
jdbcTemplate
The
JdbcTemplate that this base class manages, available to subclasses. |
protected SimpleJdbcTemplate |
simpleJdbcTemplate
Deprecated.
As of Spring 3.2, use
jdbcTemplate instead. |
private java.lang.String |
sqlScriptEncoding |
applicationContext, logger
Constructor and Description |
---|
AbstractTransactionalTestNGSpringContextTests() |
Modifier and Type | Method and Description |
---|---|
protected int |
countRowsInTable(java.lang.String tableName)
Count the rows in the given table.
|
protected int |
countRowsInTableWhere(java.lang.String tableName,
java.lang.String whereClause)
Count the rows in the given table, using the provided
WHERE clause. |
protected int |
deleteFromTables(java.lang.String... names)
Convenience method for deleting all rows from the specified tables.
|
protected void |
dropTables(java.lang.String... names)
Convenience method for dropping all of the specified tables.
|
protected void |
executeSqlScript(java.lang.String sqlResourcePath,
boolean continueOnError)
Execute the given SQL script.
|
void |
setDataSource(javax.sql.DataSource dataSource)
Set the
DataSource , typically provided via Dependency Injection. |
void |
setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.
|
run, setApplicationContext, springTestContextAfterTestClass, springTestContextAfterTestMethod, springTestContextBeforeTestClass, springTestContextBeforeTestMethod, springTestContextPrepareTestInstance
@Deprecated protected SimpleJdbcTemplate simpleJdbcTemplate
jdbcTemplate
instead.SimpleJdbcTemplate
that this base class manages, available to subclasses.protected JdbcTemplate jdbcTemplate
JdbcTemplate
that this base class manages, available to subclasses.private java.lang.String sqlScriptEncoding
public AbstractTransactionalTestNGSpringContextTests()
@Autowired public void setDataSource(javax.sql.DataSource dataSource)
DataSource
, typically provided via Dependency Injection.
This method also instantiates the simpleJdbcTemplate
and
jdbcTemplate
instance variables.
public void setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
protected int countRowsInTable(java.lang.String tableName)
tableName
- table name to count rows inprotected int countRowsInTableWhere(java.lang.String tableName, java.lang.String whereClause)
WHERE
clause.
See the Javadoc for JdbcTestUtils.countRowsInTableWhere(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.String)
for details.
tableName
- the name of the table to count rows inwhereClause
- the WHERE
clause to append to the queryWHERE
clauseprotected int deleteFromTables(java.lang.String... names)
names
- the names of the tables from which to deleteprotected void dropTables(java.lang.String... names)
names
- the names of the tables to dropprotected void executeSqlScript(java.lang.String sqlResourcePath, boolean continueOnError) throws DataAccessException
The script will normally be loaded by classpath. There should be one statement per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback.
sqlResourcePath
- the Spring resource path for the SQL scriptcontinueOnError
- whether or not to continue without throwing an
exception in the event of an errorDataAccessException
- if there is an error executing a statement
and continueOnError was false