我有一个修改一些数据库内容的 spock / spring 测试,我想知道如何回滚该记录。
目前我执行原始 sql 语句,保存字段数据,并在成功测试后恢复该数据。但我的猜测是,这可以以更简单的方式完成吗?
@ContextConfiguration(locations = "classpath*:applicationContext-test.xml")
class RepositoryTest extends Specification {
@Shared sql = Sql.newInstance("jdbc:sqlserver://...")
ResourceRepository resourceRepository;
def "Save test"() {
setup:
// copy the row before we do changes! we need to restore this later on!
def row = sql.firstRow("select id, content, status from table-name where id = 12345")
when:
...
then:
..
cleanup:
sql.execute("""
update table-name set content = ${row.content}, status = ${row.status}
where id = ${row.id}
""")
}
}