2

I am working on a web application, and I would like to run a series of integration tests, with Jetty/hsqldb. So jetty will spin up, webdriver will click around a bit and then the test will finish, leaving some junk in the database. Since I want to run more then 1 test this is problematic.

In other projects we have a function that programmatically removes rows table by table, but the database for this project is complicated, and it wouldn't be feasible to make that work here.

The best solution would be to save a known state of hsql that I can load to the database before each test.

Another solution would be to drop and recreate the tables.

In all the research I've done, I've only found vague unreproducible hints, so detail would be appreciated.

3 Answers 3

0

In our project, we use dbUnit in order to load data into our empty database for each test. The tests start a transaction and flush whatever they need to save without committing the transaction. After the test has run we check the deferred constraints and rollback. That way the database is always kept empty and tests cannot influence each other (when running serially).

For high performance execution of a series of tests using the same set of testdata, we set a savepoint after the insertion of the test data and then we only fall back to that savepoint instead of rolling back everything.

All in all, it was a lot of work. Anyway we think it paid off for our project.

http://dbunit.sourceforge.net

Sign up to request clarification or add additional context in comments.

3 Comments

Since these are front-end integration tests, I don't think I could do it as a transaction.
Good point. For that we use two databases. A master and a worker. After each test run we copy back the contents of the master to the worker (simplified).
That's an interesting way to do it. That also sounds like a lot of work to get running. I guess it's too much to hope for a session.goBackToWhatYouJustWere() method.
0

Consider the following approach:

  • Using different database for tests and for real application - for example Apache Derby database;
  • Recreate database at each run for the tests only. See this article for the details of creating Derby schema;
  • Use some tool that maintains database state as a series of changesets - Liquibase for example. With it, you can drop database each time you run the tests, and liquibase will recreate it for you.

1 Comment

Could you give a little more detail? I see no way to recreate the database in code.
0

HSQLDB has a setting for this usage.

An initial database is created with the before-test tables and data. The hsqldb.files_readonly property is set and this prevents changes to the database to be persisted to files.

This solution is often used instead of a memory test database.

populating in memory hsqldb database from script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.