类 AbstractTransactionalTestNGSpringContextTests
- java.lang.Object
- org.springframework.test.context.testng.AbstractTestNGSpringContextTests
- org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
- 所有已实现的接口:
Aware,ApplicationContextAware,org.testng.IHookable,org.testng.ITestNGListener
@TestExecutionListeners(listeners={ServletTestExecutionListener.class,DirtiesContextBeforeModesTestExecutionListener.class,DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class,TransactionalTestExecutionListener.class,SqlScriptsTestExecutionListener.class,EventPublishingTestExecutionListener.class}, inheritListeners=false) @Transactional public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests
Abstract transactional extension ofAbstractTestNGSpringContextTestswhich 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
AbstractTestNGSpringContextTests.The following
TestExecutionListenersare configured by default:- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
AbstractTestNGSpringContextTests,ContextConfiguration,TestExecutionListeners,TransactionalTestExecutionListener,SqlScriptsTestExecutionListener,Transactional,Commit,Rollback,BeforeTransaction,AfterTransaction,JdbcTestUtils,AbstractTransactionalJUnit4SpringContextTests
字段概要
字段 修饰符和类型 字段 说明 protected JdbcTemplatejdbcTemplateTheJdbcTemplatethat this base class manages, available to subclasses.从类继承的字段 org.springframework.test.context.testng.AbstractTestNGSpringContextTests
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.从类继承的方法 org.springframework.test.context.testng.AbstractTestNGSpringContextTests
run, setApplicationContext, springTestContextAfterTestClass, springTestContextAfterTestMethod, springTestContextBeforeTestClass, springTestContextBeforeTestMethod, springTestContextPrepareTestInstance
字段详细资料
jdbcTemplate
protected final JdbcTemplate jdbcTemplate
TheJdbcTemplatethat this base class manages, available to subclasses.- 从以下版本开始:
- 3.2
构造器详细资料
AbstractTransactionalTestNGSpringContextTests
public AbstractTransactionalTestNGSpringContextTests()
方法详细资料
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.- 参数:
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 providedWHEREclause.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- theWHEREclause to append to the query- 返回:
- the number of rows in the table that match the provided
WHEREclause - 从以下版本开始:
- 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 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.- 参数:
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
- 另请参阅:
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)