H2 db unit testing tip
I’ve found that occasionally the unit tests for the vle fail with database errors. Really odd once such as “Table XYZ not found”. This generally prompts for checking the test schema.
The table is being created. So why does the test fail?
It would appear the h2 is possibly a little more advanced than HSQL by default. The reason I can’t see the table is that I haven’t committed it.
By adding ‘commit;’ as the last statement to the db schema file the errors will be corrected and the tests should pass (assuming that was the only problem).
The reason for this:
In some places (especially when testing repositories or if junit runs tests in parallel) you might end up with several connections to the in memory database. This could happen when you as a repository for something and it fires a second query while retrieving data from a previous one.
The result is that the first connection where I created the tables can see them because they are a part of its working data. The second connection can’t as they haven’t been committed back to the proper database for everyone else yet.