Class AbstractTransactionalJUnit4SpringContextTests
- java.lang.Object
- org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
- org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
- All Implemented Interfaces:
Aware,ApplicationContextAware
@TestExecutionListeners({TransactionalTestExecutionListener.class,SqlScriptsTestExecutionListener.class}) @Transactional public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests
Abstract transactional extension ofAbstractJUnit4SpringContextTestswhich adds convenience functionality for JDBC access. Expects aDataSourcebean and aPlatformTransactionManagerbean to be defined in the Spring application context.This class exposes a
JdbcTemplateand 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.The following
TestExecutionListenersare configured by default:ServletTestExecutionListenerDependencyInjectionTestExecutionListenerDirtiesContextTestExecutionListenerTransactionalTestExecutionListenerSqlScriptsTestExecutionListener
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
SpringRunner,@ContextConfiguration,@TestExecutionListeners, etc. - If you wish to extend this class and use a runner other than the
SpringRunner, as of Spring Framework 4.2 you can useSpringClassRuleandSpringMethodRuleand specify your runner of choice via@RunWith(...).
NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
AbstractJUnit4SpringContextTests,ContextConfiguration,TestExecutionListeners,TransactionalTestExecutionListener,SqlScriptsTestExecutionListener,Transactional,Commit,Rollback,BeforeTransaction,AfterTransaction,JdbcTestUtils,AbstractTransactionalTestNGSpringContextTests
Field Summary
Fields Modifier and Type Field Description protected JdbcTemplatejdbcTemplateTheJdbcTemplatethat this base class manages, available to subclasses.Fields inherited from class org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
applicationContext, logger
Constructor Summary
Constructors Constructor Description AbstractTransactionalJUnit4SpringContextTests()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intcountRowsInTable(String tableName)Convenience method for counting the rows in the given table.protected intcountRowsInTableWhere(String tableName, String whereClause)Convenience method for counting the rows in the given table, using the providedWHEREclause.protected intdeleteFromTables(String... names)Convenience method for deleting all rows from the specified tables.protected intdeleteFromTableWhere(String tableName, String whereClause, Object... args)Convenience method for deleting all rows from the given table, using the providedWHEREclause.protected voiddropTables(String... names)Convenience method for dropping all of the specified tables.protected voidexecuteSqlScript(String sqlResourcePath, boolean continueOnError)Execute the given SQL script.voidsetDataSource(DataSource dataSource)Set theDataSource, typically provided via Dependency Injection.voidsetSqlScriptEncoding(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
Field Detail
jdbcTemplate
protected JdbcTemplate jdbcTemplate
TheJdbcTemplatethat 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 theDataSource, typically provided via Dependency Injection.This method also instantiates the
jdbcTemplateinstance variable.
setSqlScriptEncoding
public void setSqlScriptEncoding(String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.
countRowsInTable
protected int countRowsInTable(String tableName)
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:
JdbcTestUtils.countRowsInTable(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
countRowsInTableWhere
protected int countRowsInTableWhere(String tableName, String whereClause)
Convenience method for counting the rows in the given table, using the providedWHEREclause.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- theWHEREclause to append to the query- Returns:
- the number of rows in the table that match the provided
WHEREclause - Since:
- 3.2
- See Also:
JdbcTestUtils.countRowsInTableWhere(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.String)
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
- See Also:
JdbcTestUtils.deleteFromTables(org.springframework.jdbc.core.JdbcTemplate, java.lang.String...)
deleteFromTableWhere
protected int deleteFromTableWhere(String tableName, String whereClause, Object... args)
Convenience method for deleting all rows from the given table, using the providedWHEREclause.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- theWHEREclause to append to the queryargs- arguments to bind to the query (leaving it to thePreparedStatementto guess the corresponding SQL type); may also containSqlParameterValueobjects 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:
JdbcTestUtils.deleteFromTableWhere(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.String, java.lang.Object...)
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
- See Also:
JdbcTestUtils.dropTables(org.springframework.jdbc.core.JdbcTemplate, java.lang.String...)
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 or not to continue without throwing an exception in the event of an error- Throws:
DataAccessException- if there is an error executing a statement- See Also:
ResourceDatabasePopulator,setSqlScriptEncoding(java.lang.String)