views:

373

answers:

2

We're using hdsqldb in memory to run junit tests which operate against a database. The db is setup before running each test via a spring configuration. All works fine. Now when a tests fails it can be convinient to be able to inspect the values in the in memory database. Is this possible? If so how? Our url is:

jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true

The database is destroyed after each tests. But when the debugger is running the database should also still be alive. I've tried connecting with the sqldb databaseManager. That works, but I don't see any tables or data. Any help is highly appreciated!

+2  A: 

HSQL is in memory, so when you're say you're connecting with SQLDB Database Manager, you're not - you are connecting to another database in the memory space of the SQLDB Database Manager, not the one in the memory space of the unit test. This is why the database in the SQLDB Database Manager is empty.

You can run HSQL as a server using org.hsqldb.Server as described here.

Although the org.hsqldb.Server class is typically used to start-up a seperate process, you could instantiate and configure it in your unit test, which should allow a remote process to connect and query the database.

Alternatively, you'll have to write some sort of dump functionality that is called from within your unit test as need be.

As an aside, using HSQL in unit tests is just proving your code works against HSQL, which is different to the actual database. This means you can get false positives and vice versa. The same thing can be achieved with a mocking API or better, save the database testing for some decent integration tests that work with the real database.

Nick Holt
Thanks for your response! I've figured it out and it works if you have hsqldb write to file and use that url to connect to it. I was using an older driver which was in my way. I agree that you should also test it against a real database which we also do, but having the sql queries beeing validated is more valuable then mocking it away I'd say.
Albert
A: 

In your unit test or in the @Before / setUp() method, you can add the following line to launch the HSQL Database Manager:


org.hsqldb.util.DatabaseManager.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
});

or for the improved Swing version


org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
  "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
});

The DB manager lets you inspect your schema and run SQL queries on the live in-memory database while the application is running.

Make sure to set a beakpoint or stop the execution one way or another if you want to check the state of your database at a specific point of the execution.

dimdm
Note that I omitted the "sql.enforce_strict_size=true" part of the JDBC URL, I'm unsure what the effect would be here...
dimdm