执行 SQL 脚本
在对关系型数据库编写集成测试时,通常会运行 SQL 脚本来修改数据库模式或将测试数据插入表中,这样做会很有益。spring-jdbc
模块支持在加载 Spring ApplicationContext
时通过执行 SQL 脚本来初始化嵌入式或现有数据库。详见嵌入式数据库支持和使用嵌入式数据库测试数据访问逻辑。
尽管在加载 ApplicationContext
时一次性初始化数据库进行测试非常有用,但在集成测试期间修改数据库有时也是必不可少的。以下部分解释了如何在集成测试期间以编程方式和声明式方式运行 SQL 脚本。
以编程方式执行 SQL 脚本
Spring 提供了以下选项,用于在集成测试方法中以编程方式执行 SQL 脚本。
-
org.springframework.jdbc.datasource.init.ScriptUtils
-
org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
-
org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
-
org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
ScriptUtils
提供了一系列用于处理 SQL 脚本的静态工具方法,主要用于框架内部使用。然而,如果您需要完全控制 SQL 脚本的解析和运行方式,ScriptUtils
可能比后面描述的其他替代方案更适合您的需求。详见 ScriptUtils
中各个方法的 javadoc。
ResourceDatabasePopulator
提供了一个基于对象的 API,用于通过外部资源中定义的 SQL 脚本以编程方式填充、初始化或清理数据库。ResourceDatabasePopulator
提供了配置字符编码、语句分隔符、注释分隔符以及解析和运行脚本时使用的错误处理标志的选项。每个配置选项都有一个合理的默认值。详见 javadoc 中的默认值细节。要运行在 ResourceDatabasePopulator
中配置的脚本,您可以调用 populate(Connection)
方法针对 java.sql.Connection
运行 populator,或调用 execute(DataSource)
方法针对 javax.sql.DataSource
运行 populator。以下示例指定了用于测试模式和测试数据的 SQL 脚本,将语句分隔符设置为 @@
,并针对 DataSource
运行脚本。
-
Java
-
Kotlin
@Test
void databaseTest() {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScripts(
new ClassPathResource("test-schema.sql"),
new ClassPathResource("test-data.sql"));
populator.setSeparator("@@");
populator.execute(this.dataSource);
// run code that uses the test schema and data
}
@Test
fun databaseTest() {
val populator = ResourceDatabasePopulator()
populator.addScripts(
ClassPathResource("test-schema.sql"),
ClassPathResource("test-data.sql"))
populator.setSeparator("@@")
populator.execute(dataSource)
// run code that uses the test schema and data
}
请注意,ResourceDatabasePopulator
在内部将 SQL 脚本的解析和运行委托给 ScriptUtils
。类似地,AbstractTransactionalJUnit4SpringContextTests
和 AbstractTransactionalTestNGSpringContextTests
中的 executeSqlScript(..)
方法在内部使用 ResourceDatabasePopulator
来运行 SQL 脚本。详见各种 executeSqlScript(..)
方法的 Javadoc。
使用 @Sql 声明式执行 SQL 脚本
除了上述以编程方式运行 SQL 脚本的机制之外,您还可以在 Spring TestContext 框架中声明式地配置 SQL 脚本。具体来说,您可以在测试类或测试方法上声明 @Sql
注解,以配置在集成测试类或测试方法之前或之后针对给定数据库运行的单个 SQL 语句或 SQL 脚本的资源路径。对 @Sql
的支持由 SqlScriptsTestExecutionListener
提供,该监听器默认是启用的。
方法级别的 但是,这不适用于为 |
路径资源语义
每个路径都被解释为一个 Spring Resource
。纯路径(例如,"schema.sql"
)被视为相对于测试类定义所在的包的类路径资源。以斜杠开头的路径被视为绝对类路径资源(例如,"/org/example/schema.sql"
)。引用 URL 的路径(例如,以 classpath:
, file:
, http:
为前缀的路径)则使用指定的资源协议加载。
从 Spring Framework 6.2 开始,路径可能包含属性占位符 (${…}
),这些占位符将被测试的 ApplicationContext
的 Environment
中存储的属性替换。
以下示例展示了如何在基于 JUnit Jupiter 的集成测试类中在类级别和方法级别使用 @Sql
。
-
Java
-
Kotlin
@SpringJUnitConfig
@Sql("/test-schema.sql")
class DatabaseTests {
@Test
void emptySchemaTest() {
// run code that uses the test schema without any test data
}
@Test
@Sql({"/test-schema.sql", "/test-user-data.sql"})
void userTest() {
// run code that uses the test schema and test data
}
}
@SpringJUnitConfig
@Sql("/test-schema.sql")
class DatabaseTests {
@Test
fun emptySchemaTest() {
// run code that uses the test schema without any test data
}
@Test
@Sql("/test-schema.sql", "/test-user-data.sql")
fun userTest() {
// run code that uses the test schema and test data
}
}
默认脚本检测
如果未指定 SQL 脚本或语句,则会尝试检测一个 default
脚本,具体取决于 @Sql
的声明位置。如果无法检测到默认值,则抛出 IllegalStateException
。
-
类级别声明:如果注解的测试类是
com.example.MyTest
,则相应的默认脚本是classpath:com/example/MyTest.sql
。 -
方法级别声明:如果注解的测试方法名为
testMethod()
且定义在类com.example.MyTest
中,则相应的默认脚本是classpath:com/example/MyTest.testMethod.sql
。
记录 SQL 脚本和语句
如果您想查看哪些 SQL 脚本正在执行,请将 org.springframework.test.context.jdbc
日志类别设置为 DEBUG
。
如果您想查看哪些 SQL 语句正在执行,请将 org.springframework.jdbc.datasource.init
日志类别设置为 DEBUG
。
声明多个 @Sql
集
如果您需要为给定的测试类或测试方法配置多组 SQL 脚本,但每组需要不同的语法配置、不同的错误处理规则或不同的执行阶段,您可以声明多个 @Sql
实例。您可以使用 @Sql
作为可重复注解,或者使用 @SqlGroup
注解作为显式容器来声明多个 @Sql
实例。
以下示例展示了如何将 @Sql
用作可重复注解。
-
Java
-
Kotlin
@Test
@Sql(scripts = "/test-schema.sql", config = @SqlConfig(commentPrefix = "`"))
@Sql("/test-user-data.sql")
void userTest() {
// run code that uses the test schema and test data
}
@Test
@Sql("/test-schema.sql", config = SqlConfig(commentPrefix = "`"))
@Sql("/test-user-data.sql")
fun userTest() {
// run code that uses the test schema and test data
}
在前面的示例所示的场景中,test-schema.sql
脚本对单行注释使用了不同的语法。
以下示例与前面的示例完全相同,只是 @Sql
声明被组合在 @SqlGroup
中。使用 @SqlGroup
是可选的,但为了与其他 JVM 语言兼容,您可能需要使用 @SqlGroup
。
-
Java
-
Kotlin
@Test
@SqlGroup({
@Sql(scripts = "/test-schema.sql", config = @SqlConfig(commentPrefix = "`")),
@Sql("/test-user-data.sql")
)}
void userTest() {
// run code that uses the test schema and test data
}
@Test
@SqlGroup(
Sql("/test-schema.sql", config = SqlConfig(commentPrefix = "`")),
Sql("/test-user-data.sql")
)
fun userTest() {
// Run code that uses the test schema and test data
}
脚本执行阶段
默认情况下,SQL 脚本在相应的测试方法之前运行。但是,如果您需要在测试方法之后运行特定的脚本集(例如,清理数据库状态),您可以将 @Sql
中的 executionPhase
属性设置为 AFTER_TEST_METHOD
,如下例所示。
-
Java
-
Kotlin
@Test
@Sql(
scripts = "create-test-data.sql",
config = @SqlConfig(transactionMode = ISOLATED)
)
@Sql(
scripts = "delete-test-data.sql",
config = @SqlConfig(transactionMode = ISOLATED),
executionPhase = AFTER_TEST_METHOD
)
void userTest() {
// run code that needs the test data to be committed
// to the database outside of the test's transaction
}
@Test
@Sql("create-test-data.sql",
config = SqlConfig(transactionMode = ISOLATED))
@Sql("delete-test-data.sql",
config = SqlConfig(transactionMode = ISOLATED),
executionPhase = AFTER_TEST_METHOD)
fun userTest() {
// run code that needs the test data to be committed
// to the database outside of the test's transaction
}
ISOLATED 和 AFTER_TEST_METHOD 分别从 Sql.TransactionMode 和 Sql.ExecutionPhase 中静态导入。 |
从 Spring Framework 6.1 开始,可以通过将类级别 @Sql
声明中的 executionPhase
属性设置为 BEFORE_TEST_CLASS
或 AFTER_TEST_CLASS
来在测试类之前或之后运行特定的脚本集,如下例所示。
-
Java
-
Kotlin
@SpringJUnitConfig
@Sql(scripts = "/test-schema.sql", executionPhase = BEFORE_TEST_CLASS)
class DatabaseTests {
@Test
void emptySchemaTest() {
// run code that uses the test schema without any test data
}
@Test
@Sql("/test-user-data.sql")
void userTest() {
// run code that uses the test schema and test data
}
}
@SpringJUnitConfig
@Sql("/test-schema.sql", executionPhase = BEFORE_TEST_CLASS)
class DatabaseTests {
@Test
fun emptySchemaTest() {
// run code that uses the test schema without any test data
}
@Test
@Sql("/test-user-data.sql")
fun userTest() {
// run code that uses the test schema and test data
}
}
BEFORE_TEST_CLASS 从 Sql.ExecutionPhase 中静态导入。 |
使用 @SqlConfig
进行脚本配置
您可以使用 @SqlConfig
注解来配置脚本解析和错误处理。当在集成测试类上声明为类级别注解时,@SqlConfig
用作测试类层次结构中所有 SQL 脚本的全局配置。当直接使用 @Sql
注解的 config
属性声明时,@SqlConfig
用作包含的 @Sql
注解中声明的 SQL 脚本的本地配置。@SqlConfig
中的每个属性都有一个隐式的默认值,这在相应属性的 javadoc 中有详细说明。由于 Java 语言规范中对注解属性定义的规则,不幸的是,无法将 null
值分配给注解属性。因此,为了支持覆盖继承的全局配置,@SqlConfig
属性具有显式的默认值,可以是 ""
(对于 String)、{}
(对于数组)或 DEFAULT
(对于枚举)。这种方法允许本地 @SqlConfig
声明通过提供不同于 ""
、{}
或 DEFAULT
的值来选择性地覆盖全局 @SqlConfig
声明中的单个属性。当本地 @SqlConfig
属性没有提供显式值(不同于 ""
、{}
或 DEFAULT
)时,全局 @SqlConfig
属性会被继承。因此,显式的本地配置会覆盖全局配置。
@Sql
和 @SqlConfig
提供的配置选项与 ScriptUtils
和 ResourceDatabasePopulator
支持的选项等价,但它们是 <jdbc:initialize-database/>
XML 命名空间元素提供的选项的超集。详见 @Sql
和 @SqlConfig
中各个属性的 javadoc。
@Sql
的事务管理
默认情况下,SqlScriptsTestExecutionListener
会推断使用 @Sql
配置的脚本所需的事务语义。具体来说,SQL 脚本可以在无事务、现有 Spring 管理的事务内(例如,由为 @Transactional
注解的测试管理的事务),或隔离事务内运行,具体取决于 @SqlConfig
中 transactionMode
属性的配置值以及测试的 ApplicationContext
中是否存在 PlatformTransactionManager
。然而,最基本的要求是测试的 ApplicationContext
中必须存在一个 javax.sql.DataSource
。
如果 SqlScriptsTestExecutionListener
用于检测 DataSource
和 PlatformTransactionManager
并推断事务语义的算法不符合您的需求,您可以通过设置 @SqlConfig
的 dataSource
和 transactionManager
属性来指定显式名称。此外,您还可以通过设置 @SqlConfig
的 transactionMode
属性来控制事务传播行为(例如,脚本是否应在隔离事务中运行)。尽管对 @Sql
事务管理所有支持选项的详细讨论超出了本参考手册的范围,但 @SqlConfig
和 SqlScriptsTestExecutionListener
的 javadoc 提供了详细信息,并且以下示例展示了一个使用 JUnit Jupiter 和 @Sql
进行事务性测试的典型测试场景。
-
Java
-
Kotlin
@SpringJUnitConfig(TestDatabaseConfig.class)
@Transactional
class TransactionalSqlScriptsTests {
final JdbcTemplate jdbcTemplate;
@Autowired
TransactionalSqlScriptsTests(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Test
@Sql("/test-data.sql")
void usersTest() {
// verify state in test database:
assertNumUsers(2);
// run code that uses the test data...
}
int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
void assertNumUsers(int expected) {
assertEquals(expected, countRowsInTable("user"),
"Number of rows in the [user] table.");
}
}
@SpringJUnitConfig(TestDatabaseConfig::class)
@Transactional
class TransactionalSqlScriptsTests @Autowired constructor(dataSource: DataSource) {
val jdbcTemplate: JdbcTemplate = JdbcTemplate(dataSource)
@Test
@Sql("/test-data.sql")
fun usersTest() {
// verify state in test database:
assertNumUsers(2)
// run code that uses the test data...
}
fun countRowsInTable(tableName: String): Int {
return JdbcTestUtils.countRowsInTable(jdbcTemplate, tableName)
}
fun assertNumUsers(expected: Int) {
assertEquals(expected, countRowsInTable("user"),
"Number of rows in the [user] table.")
}
}
请注意,在 usersTest()
方法运行后无需清理数据库,因为对数据库所做的任何更改(无论是在测试方法内还是在 /test-data.sql
脚本内)都会被 TransactionalTestExecutionListener
自动回滚(详见事务管理)。
使用 @SqlMergeMode
合并和覆盖配置
可以将方法级别的 @Sql
声明与类级别的声明合并。例如,这允许您为数据库模式或某些通用测试数据在每个测试类中提供一次配置,然后在每个测试方法中提供额外的、特定于用例的测试数据。要启用 @Sql
合并,请使用 @SqlMergeMode(MERGE)
注解您的测试类或测试方法。要为特定测试方法(或特定测试子类)禁用合并,您可以通过 @SqlMergeMode(OVERRIDE)
切换回默认模式。有关示例和更多详细信息,请查阅@SqlMergeMode
注解文档部分。