Class AbstractTransactionalTestNGSpringContextTests

  extended by org.springframework.test.context.testng.AbstractTestNGSpringContextTests
      extended by org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
All Implemented Interfaces:
Aware, ApplicationContextAware

public abstract class AbstractTransactionalTestNGSpringContextTests
extends AbstractTestNGSpringContextTests

Abstract transactional extension of 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 SimpleJdbcTemplate and provides an easy way to count the number of rows in a table , delete from the database , and execute SQL scripts within a transaction.

Concrete subclasses must fulfill the same requirements outlined in AbstractTestNGSpringContextTests.

Sam Brannen, Juergen Hoeller
See Also:
AbstractTestNGSpringContextTests, ContextConfiguration, TestExecutionListeners, TransactionalTestExecutionListener, TransactionConfiguration, Transactional, NotTransactional, Rollback, SimpleJdbcTestUtils, AbstractTransactionalJUnit38SpringContextTests, AbstractTransactionalJUnit4SpringContextTests

Field Summary
protected  SimpleJdbcTemplate simpleJdbcTemplate
          The SimpleJdbcTemplate that this base class manages, available to subclasses.
private  java.lang.String sqlScriptEncoding
Fields inherited from class org.springframework.test.context.testng.AbstractTestNGSpringContextTests
applicationContext, logger
Constructor Summary
Method Summary
protected  int countRowsInTable(java.lang.String tableName)
          Count the rows in the given table.
protected  int deleteFromTables(java.lang.String... names)
          Convenience method for deleting all rows from 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.
Methods inherited from class org.springframework.test.context.testng.AbstractTestNGSpringContextTests
run, setApplicationContext, springTestContextAfterTestClass, springTestContextAfterTestMethod, springTestContextBeforeTestClass, springTestContextBeforeTestMethod, springTestContextPrepareTestInstance
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


protected SimpleJdbcTemplate simpleJdbcTemplate
The SimpleJdbcTemplate that this base class manages, available to subclasses.


private java.lang.String sqlScriptEncoding
Constructor Detail


public AbstractTransactionalTestNGSpringContextTests()
Method Detail


public void setDataSource(javax.sql.DataSource dataSource)
Set the DataSource, typically provided via Dependency Injection.

dataSource - the DataSource to inject


public void setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.

See Also:
executeSqlScript(java.lang.String, boolean)


protected int countRowsInTable(java.lang.String tableName)
Count the rows in the given table.

tableName - table name to count rows in
the number of rows in the table


protected int deleteFromTables(java.lang.String... names)
Convenience method for deleting all rows from the specified tables. Use with caution outside of a transaction!

names - the names of the tables from which to delete
the total number of rows deleted from all specified tables


protected void executeSqlScript(java.lang.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. 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 script
continueOnError - whether or not to continue without throwing an exception in the event of an error
DataAccessException - if there is an error executing a statement and continueOnError was false