类 AbstractTransactionalJUnit4SpringContextTests
- java.lang.Object
- org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
- org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
- 所有已实现的接口:
Aware
,ApplicationContextAware
@TestExecutionListeners({TransactionalTestExecutionListener.class,SqlScriptsTestExecutionListener.class}) @Transactional public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests
Abstract transactional extension ofAbstractJUnit4SpringContextTests
which adds convenience functionality for JDBC access. Expects aDataSource
bean and aPlatformTransactionManager
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
.The following
TestExecutionListeners
are configured by default:ServletTestExecutionListener
DependencyInjectionTestExecutionListener
DirtiesContextTestExecutionListener
TransactionalTestExecutionListener
SqlScriptsTestExecutionListener
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 useSpringClassRule
andSpringMethodRule
and specify your runner of choice via@RunWith(...)
.
NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
AbstractJUnit4SpringContextTests
,ContextConfiguration
,TestExecutionListeners
,TransactionalTestExecutionListener
,SqlScriptsTestExecutionListener
,Transactional
,Commit
,Rollback
,BeforeTransaction
,AfterTransaction
,JdbcTestUtils
,AbstractTransactionalTestNGSpringContextTests
字段概要
字段 修饰符和类型 字段 说明 protected JdbcTemplate
jdbcTemplate
TheJdbcTemplate
that this base class manages, available to subclasses.从类继承的字段 org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
applicationContext, logger
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected 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 of 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.从类继承的方法 org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
setApplicationContext
字段详细资料
jdbcTemplate
protected JdbcTemplate jdbcTemplate
TheJdbcTemplate
that this base class manages, available to subclasses.- 从以下版本开始:
- 3.2
构造器详细资料
AbstractTransactionalJUnit4SpringContextTests
public AbstractTransactionalJUnit4SpringContextTests()
方法详细资料
setDataSource
@Autowired public void setDataSource(DataSource dataSource)
Set theDataSource
, typically provided via Dependency Injection.This method also instantiates the
jdbcTemplate
instance 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.- 参数:
tableName
- table name to count rows in- 返回:
- the number of rows in the table
- 另请参阅:
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 providedWHERE
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
- theWHERE
clause to append to the query- 返回:
- the number of rows in the table that match the provided
WHERE
clause - 从以下版本开始:
- 3.2
- 另请参阅:
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!
- 参数:
names
- the names of the tables from which to delete- 返回:
- the total number of rows deleted from all specified tables
- 另请参阅:
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 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.- 参数:
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.- 返回:
- the number of rows deleted from the table
- 从以下版本开始:
- 4.0
- 另请参阅:
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!
- 参数:
names
- the names of the tables to drop- 从以下版本开始:
- 3.2
- 另请参阅:
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.
- 参数:
sqlResourcePath
- the Spring resource path for the SQL scriptcontinueOnError
- whether or not to continue without throwing an exception in the event of an error- 抛出:
DataAccessException
- if there is an error executing a statement- 另请参阅:
ResourceDatabasePopulator
,setSqlScriptEncoding(java.lang.String)