org.springframework.test.context.junit4

## Class AbstractTransactionalJUnit4SpringContextTests

- java.lang.Object
- - org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
  - - org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests

All Implemented Interfaces:
  Aware, ApplicationContextAware

    @TestExecutionListeners(value=TransactionalTestExecutionListener.class)
    @Transactional
    public abstract class AbstractTransactionalJUnit4SpringContextTests
    extends AbstractJUnit4SpringContextTests

Abstract transactional extension of `AbstractJUnit4SpringContextTests` 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 `AbstractJUnit4SpringContextTests`.

Note: this class serves only as a convenience for extension. If you do not wish for your test classes to be tied to a Spring-specific class hierarchy, you may configure your own custom test classes by using `SpringJUnit4ClassRunner`, `@ContextConfiguration`, `@TestExecutionListeners`, `@Transactional`, etc.

Since:
  2.5
Author:
  Sam Brannen, Juergen Hoeller
See Also:
  `AbstractJUnit4SpringContextTests`, `ContextConfiguration`, `TestExecutionListeners`, `TransactionalTestExecutionListener`, `TransactionConfiguration`, `Transactional`, `NotTransactional`, `Rollback`, `BeforeTransaction`, `AfterTransaction`, `JdbcTestUtils`, `AbstractTransactionalTestNGSpringContextTests`

### Field Summary

Fields
`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.

### Fields inherited from class org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests

`applicationContext, logger`

### Constructor Summary

Constructors
Constructor and Description
`AbstractTransactionalJUnit4SpringContextTests()`

### Method Summary

Methods
`protected int` `countRowsInTable(String tableName)` Count the rows in the given table.
`protected int` `countRowsInTableWhere(String tableName, String whereClause)` Count the rows in the given table, using the provided `WHERE` clause.
`protected int` `deleteFromTables(String... names)` Convenience method for deleting all rows from the specified tables.
`protected void` `dropTables(String... names)` Convenience method for dropping all of the specified tables.
`protected void` `executeSqlScript(String sqlResourcePath, boolean continueOnError)` Execute the given SQL script.
`void` `setDataSource(DataSource dataSource)` Set the `DataSource`, 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.junit4.AbstractJUnit4SpringContextTests

`setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### simpleJdbcTemplate

    @Deprecated
    protected SimpleJdbcTemplate simpleJdbcTemplate

Deprecated. As of Spring 3.2, use `jdbcTemplate` instead.
The `SimpleJdbcTemplate` that this base class manages, available to subclasses.

#### jdbcTemplate

    protected JdbcTemplate jdbcTemplate

The `JdbcTemplate` that this base class manages, available to subclasses.

Since:
  3.2

### Constructor Detail

#### AbstractTransactionalJUnit4SpringContextTests

    public AbstractTransactionalJUnit4SpringContextTests()

### Method Detail

#### setDataSource

    @Autowired
    public void setDataSource(DataSource dataSource)

Set the `DataSource`, typically provided via Dependency Injection.

This method also instantiates the `simpleJdbcTemplate` and `jdbcTemplate` instance variables.

#### setSqlScriptEncoding

    public void setSqlScriptEncoding(String sqlScriptEncoding)

Specify the encoding for SQL scripts, if different from the platform encoding.

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

#### countRowsInTable

    protected int countRowsInTable(String tableName)

Count the rows in the given table.

Parameters:
  `tableName` - table name to count rows in
Returns:
  the number of rows in the table

#### countRowsInTableWhere

    protected int countRowsInTableWhere(String tableName,
                            String whereClause)

Count the rows in the given table, using the provided `WHERE` 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 in
  `whereClause` - the `WHERE` clause to append to the query
Returns:
  the number of rows in the table that match the provided `WHERE` clause
Since:
  3.2

#### deleteFromTables

    protected int deleteFromTables(String... names)

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

#### dropTables

    protected void dropTables(String... names)

Convenience method for dropping all of the specified tables. Use with caution outside of a transaction!

Parameters:
  `names` - the names of the tables to drop
Since:
  3.2

#### 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. There should be one statement per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback.

Parameters:
  `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
Throws:
  `DataAccessException` - if there is an error executing a statement and continueOnError was `false`