Class AbstractTransactionalTestNGSpringContextTests
- All Implemented Interfaces:
Aware
,ApplicationContextAware
,org.testng.IHookable
,org.testng.ITestNGListener
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
.
The following TestExecutionListeners
are configured by default:
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected final JdbcTemplate
TheJdbcTemplate
that this base class manages, available to subclasses.Fields inherited from class org.springframework.test.context.testng.AbstractTestNGSpringContextTests
applicationContext, logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
countRowsInTable
(String tableName) Convenience method for counting the rows in the given table.protected int
countRowsInTableWhere
(String tableName, String whereClause) Convenience method for counting the rows in the given table, using the providedWHERE
clause.protected int
deleteFromTables
(String... names) Convenience method for deleting all rows from the specified tables.protected int
deleteFromTableWhere
(String tableName, String whereClause, Object... args) Convenience method for deleting all rows from the given table, using the providedWHERE
clause.protected void
dropTables
(String... names) Convenience method for dropping all the specified tables.protected void
executeSqlScript
(String sqlResourcePath, boolean continueOnError) Execute the given SQL script.void
setDataSource
(DataSource dataSource) Set theDataSource
, typically provided via Dependency Injection.void
setSqlScriptEncoding
(String sqlScriptEncoding) Specify the encoding for SQL scripts, if different from the platform encoding.Methods inherited from class org.springframework.test.context.testng.AbstractTestNGSpringContextTests
run, setApplicationContext, springTestContextAfterTestClass, springTestContextAfterTestMethod, springTestContextBeforeTestClass, springTestContextBeforeTestMethod, springTestContextPrepareTestInstance
-
Field Details
-
jdbcTemplate
TheJdbcTemplate
that this base class manages, available to subclasses.- Since:
- 3.2
-
-
Constructor Details
-
AbstractTransactionalTestNGSpringContextTests
public AbstractTransactionalTestNGSpringContextTests()
-
-
Method Details
-
setDataSource
Set theDataSource
, typically provided via Dependency Injection.This method also instantiates the
jdbcTemplate
instance variable. -
setSqlScriptEncoding
Specify the encoding for SQL scripts, if different from the platform encoding. -
countRowsInTable
Convenience method for counting the rows in the given table.- Parameters:
tableName
- table name to count rows in- Returns:
- the number of rows in the table
- See Also:
-
countRowsInTableWhere
Convenience method for counting the rows in the given table, using the providedWHERE
clause.See the Javadoc for
JdbcTestUtils.countRowsInTableWhere(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.String)
for details.- Parameters:
tableName
- the name of the table to count rows inwhereClause
- theWHERE
clause to append to the query- Returns:
- the number of rows in the table that match the provided
WHERE
clause - Since:
- 3.2
- See Also:
-
deleteFromTables
Convenience method for deleting all rows from the specified tables.Use with caution outside of a transaction!
- Parameters:
names
- the names of the tables from which to delete- Returns:
- the total number of rows deleted from all specified tables
- See Also:
-
deleteFromTableWhere
Convenience method for deleting all rows from the given table, using the providedWHERE
clause.Use with caution outside of a transaction!
See the Javadoc for
JdbcTestUtils.deleteFromTableWhere(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.String, java.lang.Object...)
for details.- Parameters:
tableName
- the name of the table to delete rows fromwhereClause
- theWHERE
clause to append to the queryargs
- arguments to bind to the query (leaving it to thePreparedStatement
to guess the corresponding SQL type); may also containSqlParameterValue
objects which indicate not only the argument value but also the SQL type and optionally the scale.- Returns:
- the number of rows deleted from the table
- Since:
- 4.0
- See Also:
-
dropTables
Convenience method for dropping all the specified tables.Use with caution outside of a transaction!
- Parameters:
names
- the names of the tables to drop- Since:
- 3.2
- See Also:
-
executeSqlScript
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException Execute the given SQL script.Use with caution outside of a transaction!
The script will normally be loaded by classpath.
Do not use this method to execute DDL if you expect rollback.
- Parameters:
sqlResourcePath
- the Spring resource path for the SQL scriptcontinueOnError
- whether to continue without throwing an exception in the event of an error- Throws:
DataAccessException
- if there is an error executing a statement- See Also:
-