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
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
@Beforesetup method, rather than in a@Afterteardown 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
@Beforesetup method, and to roll it back in the@Afterteardown method. This would depend on how your data access layer is written.If your data access layer accepts
Connectionobjects, 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.