类 AbstractTransactionalJUnit4ContextTests
- 所有已实现的接口:
cn.taketoday.beans.factory.Aware,cn.taketoday.context.ApplicationContextAware
AbstractJUnit4ContextTests which adds convenience functionality
for JDBC access. Expects a DataSource bean and a
PlatformTransactionManager bean to be defined in the Infra
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
AbstractJUnit4ContextTests.
This class serves only as a convenience for extension.
- If you do not wish for your test classes to be tied to a Infra-specific
class hierarchy, you may configure your own custom test classes by using
InfraRunner,@ContextConfiguration,@TestExecutionListeners, etc. - If you wish to extend this class and use a runner other than the
InfraRunner, you can useApplicationClassRuleandApplicationMethodRuleand specify your runner of choice via@RunWith(...).
NOTE: This class requires JUnit 4.12 or higher.
- 从以下版本开始:
- 4.0
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明protected final cn.taketoday.jdbc.core.JdbcTemplateTheJdbcTemplatethat this base class manages, available to subclasses.private String从类继承的字段 cn.taketoday.test.context.junit4.AbstractJUnit4ContextTests
applicationContext, logger -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明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.从类继承的方法 cn.taketoday.test.context.junit4.AbstractJUnit4ContextTests
setApplicationContext
-
字段详细资料
-
jdbcTemplate
protected final cn.taketoday.jdbc.core.JdbcTemplate jdbcTemplateTheJdbcTemplatethat this base class manages, available to subclasses.- 从以下版本开始:
- 4.0
-
sqlScriptEncoding
-
-
构造器详细资料
-
AbstractTransactionalJUnit4ContextTests
public AbstractTransactionalJUnit4ContextTests()
-
-
方法详细资料
-
setDataSource
Set theDataSource, typically provided via Dependency Injection.This method also instantiates the
jdbcTemplateinstance 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.- 参数:
tableName- table name to count rows in- 返回:
- the number of rows in the table
- 另请参阅:
-
countRowsInTableWhere
Convenience method for counting the rows in the given table, using the providedWHEREclause.See the Javadoc for
JdbcTestUtils.countRowsInTableWhere(cn.taketoday.jdbc.core.JdbcOperations, java.lang.String, java.lang.String)for details.- 参数:
tableName- the name of the table to count rows inwhereClause- theWHEREclause to append to the query- 返回:
- the number of rows in the table that match the provided
WHEREclause - 从以下版本开始:
- 4.0
- 另请参阅:
-
deleteFromTables
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
- 另请参阅:
-
deleteFromTableWhere
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(cn.taketoday.jdbc.core.JdbcOperations, java.lang.String, java.lang.String, java.lang.Object...)for details.- 参数:
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.- 返回:
- the number of rows deleted from the table
- 从以下版本开始:
- 4.0
- 另请参阅:
-
dropTables
Convenience method for dropping all of the specified tables.Use with caution outside of a transaction!
- 参数:
names- the names of the tables to drop- 从以下版本开始:
- 4.0
- 另请参阅:
-
executeSqlScript
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws cn.taketoday.dao.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 Infra resource path for the SQL scriptcontinueOnError- whether or not to continue without throwing an exception in the event of an error- 抛出:
cn.taketoday.dao.DataAccessException- if there is an error executing a statement- 另请参阅:
-
ResourceDatabasePopulatorsetSqlScriptEncoding(java.lang.String)
-