Spring TestContext是Spring提供的一套基于注解的Test框架,Spring TestContext有非常好的兼容性,可以无缝兼容JUnit,TestNG等单元测试框架,而且在其基础上增加更多的功能
在Spring应用大行其道的今天,使用Spring来构建应用已经是再普通不过的事情,但当使用JUnit,TestNG等传统的单元测试技术和Spring进行结合的时候,就会出现很多并不如意的事情
还好,Spring 提供了单元测试的强大支持,主要特性包括:
TestContext 测试框架的核心由 org.springframework.test.context 包中三个类组成,分别是 TestContext 和 TestContextManager 类以及 TestExecutionListener 接口。
Spring TestContext 允许在测试用例类中通过 @TestExecutionListeners 注解向 TestContextManager 注册多个监听器
Spring 提供了几个 TestExecutionListener 接口实现类,分别说明如下:
JUnit 4 中可以通过 @RunWith 注解指定测试用例的运行器,Spring TestContext 框架提供了扩展于 org.junit.internal.runners.JUnit4ClassRunner 的 SpringJUnit4ClassRunner 运行器,它负责总装 Spring TestContext 测试框架并将其统一到 JUnit 4 框架中。
Spring TestContext 为基于 JUnit 4.4 测试框架提供了两个抽象测试用例类,分别是 AbstractJUnit4SpringContextTests 和 AbstractTransactionalJUnit4SpringContextTests,而后者扩展于前者。让我们来看一下这两个抽象测试用例类的代码:
@RunWith(SpringJUnit4ClassRunner.class) //指定测试用例运行器
//注册了两个TestExecutionListener监听器
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
/**
* Logger available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
/**
* The {@link ApplicationContext} that was injected into this test instance
* via {@link #setApplicationContext(ApplicationContext)}.
*/
protected ApplicationContext applicationContext;
/**
* Set the {@link ApplicationContext} to be used by this test instance,
* provided via {@link ApplicationContextAware} semantics.
*/
public final void setApplicationContext(final ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
AbstractTransactionalJUnit4SpringContextTests 扩展于 AbstractJUnit4SpringContextTests,提供了事务管理的支持
//注册测试用例事务管理的监听器
@TestExecutionListeners(TransactionalTestExecutionListener.class)
//使测试用例的所有方法都将工作于事务环境下
@Transactional
@SuppressWarnings("deprecation")
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {
/**
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
*/
@Deprecated
protected SimpleJdbcTemplate simpleJdbcTemplate;
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
*/
protected JdbcTemplate jdbcTemplate;
private String sqlScriptEncoding;
/**
* Set the {@code DataSource}, typically provided via Dependency Injection.
* This method also instantiates the {@link #simpleJdbcTemplate} and
* {@link #jdbcTemplate} instance variables.
*/
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/**
* Specify the encoding for SQL scripts, if different from the platform encoding.
* @see #executeSqlScript
*/
public void setSqlScriptEncoding(String sqlScriptEncoding) {
this.sqlScriptEncoding = sqlScriptEncoding;
}
/**
* Count the rows in the given table.
* @param tableName table name to count rows in
* @return the number of rows in the table
*/
protected int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
/**
* Count the rows in the given table, using the provided {@code WHERE} clause.
*
See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere} for details.
* @param tableName the name of the table to count rows in
* @param whereClause the {@code WHERE} clause to append to the query
* @return the number of rows in the table that match the provided
* {@code WHERE} clause
* @since 3.2
*/
protected int countRowsInTableWhere(String tableName, String whereClause) {
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
}
/**
* Convenience method for deleting all rows from the specified tables. Use
* with caution outside of a transaction!
* @param names the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
*/
protected int deleteFromTables(String... names) {
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
}
/**
* Convenience method for dropping all of the specified tables. Use
* with caution outside of a transaction!
* @param names the names of the tables to drop
* @since 3.2
*/
protected void dropTables(String... names) {
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
}
/**
* 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.
* @param sqlResourcePath the Spring resource path for the SQL script
* @param 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 {@code false}
*/
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = this.applicationContext.getResource(sqlResourcePath);
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
this.sqlScriptEncoding), continueOnError);
}
}
引入对junit以及spring-test库的依赖
junit
junit
${junitVersion}
org.springframework
spring-test
${org.springframework-version}
test
Spring 的 TestContext 测试框架不但可以整合到 JUnit 4.4 测试框架上,而且还可以整合到 JUnit 3.8 以及 TestNG 等测试框架上。目前已经提供了对 JUnit 3.8 以及 TestNG 的支持,你可以分别在 org.springframework.test.context.junit38 和 org.springframework.test.context.testng 包下找到整合的帮助类。