Spring 数据 Jpa。如何在特定测试 类 的 运行 单元测试之前从存储库中清除数据?
Spring Data Jpa. How to cleaning data from repositories befor run unit test from particular test classes?
我在 spring data jpa 中编写的持久性单元测试有问题。
对于特定的存储库,我进行了单元测试以确保一切正常。我也有一个集成测试。当我 运行 它用于特定测试 类 时,每个测试都通过了。但是当我 运行 一整套测试时,我遇到了很多失败,因为我有记录从以前的测试中插入到数据库中。
当然在每个测试中 类 我可以添加 @After 方法来清理每个数据,但我想问一下是否可以在特定测试的 运行 测试之前从数据库中清理所有数据类 没有添加@After 方法?
此致。
使用Spring的事务测试支持确保在每次测试后回滚数据库更改:
One common issue in tests that access a real database is their effect
on the state of the persistence store. Even when you’re using a
development database, changes to the state may affect future tests.
Also, many operations — such as inserting or modifying persistent
data — cannot be performed (or verified) outside a transaction.
The TestContext framework addresses this issue. By default, the
framework will create and roll back a transaction for each test.
我在 spring data jpa 中编写的持久性单元测试有问题。
对于特定的存储库,我进行了单元测试以确保一切正常。我也有一个集成测试。当我 运行 它用于特定测试 类 时,每个测试都通过了。但是当我 运行 一整套测试时,我遇到了很多失败,因为我有记录从以前的测试中插入到数据库中。
当然在每个测试中 类 我可以添加 @After 方法来清理每个数据,但我想问一下是否可以在特定测试的 运行 测试之前从数据库中清理所有数据类 没有添加@After 方法?
此致。
使用Spring的事务测试支持确保在每次测试后回滚数据库更改:
One common issue in tests that access a real database is their effect on the state of the persistence store. Even when you’re using a development database, changes to the state may affect future tests. Also, many operations — such as inserting or modifying persistent data — cannot be performed (or verified) outside a transaction.
The TestContext framework addresses this issue. By default, the framework will create and roll back a transaction for each test.