每个测试用例后清除内存数据库
我正在使用 hsqldb 来测试 Java 中的一些数据访问层。我有大约 100 个测试用例。我创建一个内存数据库,然后在表中插入一些值,以便通过我的测试用例我可以加载它,但问题是对于每个测试用例,我需要清除内存数据库,只有值而不是表。
是否可能,一件事是我需要手动删除表中的行,还有其他我可以使用的东西吗?
谢谢
I am using hsqldb for testing some of the data access layer in Java. I have certain test cases like 100 around. I create a in memory database and then insert some values in the table so that with my test case i can load it, but the problem is for every test case i need to clear the in memory database, only the values not the tables.
Is it possible, one thing is i need to manually delete the rows from the table and is there some thing else I can use.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在单元测试中使用DbUnit,则可以指定DbUnit应在之前执行清理和插入操作每次测试前确保数据库内容处于有效状态。这可以通过类似于下面的方式来完成:
请注意,始终建议在
@Before
设置方法中执行任何设置活动,而不是在@After
中> 拆解方法。后者表明您正在正在测试的方法中创建新的数据库对象,恕我直言,这并不容易实现可测试的行为。此外,如果您在测试后进行清理,以确保第二个测试正确运行,那么任何此类清理实际上都是第二个测试设置的一部分,而不是第一个测试的拆卸。使用 DbUnit 的替代方法是在
@Before
设置方法中启动一个新事务,并在@After
拆卸方法中将其回滚。这取决于您的数据访问层的编写方式。如果您的数据访问层接受
Connection
对象,那么您的设置例程应该创建它们,并关闭自动提交。此外,还假设您的数据访问层不会调用Connection.commit
。假设前面的情况,您可以在拆卸方法中使用Connection.rollback()
回滚事务。关于事务控制,下面的代码片段演示了如何使用 JPA 来做到这一点:
对于其他 ORM 框架甚至您的自定义持久层(如果您已经编写了一个)也必须采取类似的方法。
If you use DbUnit in unit-tests, you can specify that DbUnit should perform a clean-and-insert operation before every test to ensure that the contents of the database are in a valid state before every test. This can be done in a manner similar to the one below:
Note that it is always recommended to perform any setup activities in a
@Before
setup method, rather than in a@After
teardown method. The latter indicates that you are creating new database objects in a method being tested, which IMHO does not exactly lend easily to testable behavior. Besides, if you are cleaning up after a test, to ensure that a second test runs correctly, then any such cleanup is actually a part of the setup of the second test, and not a teardown of the first.The alternative to using DbUnit is to start a new transaction in your
@Before
setup method, and to roll it back in the@After
teardown method. This would depend on how your data access layer is written.If your data access layer accepts
Connection
objects, then your setup routine should create them, and turn off auto-commit. Also, there is an assumption that your data access layer will not invokeConnection.commit
. Assuming the previous, you can rollback the transaction usingConnection.rollback()
in your teardown method.With respect to transaction control, the below snippet demonstrates how one would do it using JPA for instance:
Similar approaches would have to be undertaken for other ORM frameworks or even your custom persistence layer, if you have written one.
您可以使用HSQLDB事务吗?
在每次测试之前,启动一个新事务:
在每次测试之后,将其回滚:
这也将允许您拥有一些永久数据。
Could you use HSQLDB transactions?
Before every test, start a new transaction:
After every test, roll it back:
This would also allow you to have some permanent data.
根据您的测试框架,可以在每次测试后执行删除调用。在 Junit 中,注释是 @After 并且带有此注释的方法将是在每个 [
@Test
] 方法之后运行。Depending on your test framework, it is possible to execute a delete call after each test. In Junit the annotation is @After and a method with this annotation will be run after each [
@Test
] method.您必须使用截断查询来销毁数据库内存,否则此链接会对您有所帮助。
http://wiki.apache.org/db-derby/InMemoryBackEndPrimer
You have to use Truncate Query for the Destroy Database Memory or this link can be helpful to you.
http://wiki.apache.org/db-derby/InMemoryBackEndPrimer