I want to create unit tests that cover code that use relational database in Play framework 2.1.0. There are many possibilities for this and all cause problems:
Testing on in-memory H2 database
Play framework documentation proposes to run unit tests on H2 in-memory database, even if main database used for development and production use other software (i.e. MySQL):
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
My application don't use complicated RDBMS features such as stored procedures and most database access cases are ebean calls, so it should be compatible with both MySQL and H2.
However, table creation statements in evolutions use MySQL-specific features, such as specifying ENGINE = InnoDB
, DEFAULT CHARACTER SET = utf8
, etc. I fear if I will remove these proprietary parts of CREATE TABLE
, MySQL will use some default setting that I can't control and that depend on version, so to test and develop application main MySQL config must be modified.
Anybody used this approach (making evolutions compatible with both MySQL and H2)?
Other ideas how it can be handled:
- Separate evolutions for MySQL and H2 (not a good idea)
- Some way to make H2 ignore additional MySQL stuff in
create table
(MySQL compatibility mode don't work, it still complain even ondefault character set
). I don't know how.
Testing on the same database driver as main database
The only advantage of H2 in-memory database that it is fast, and testing on the same database driver than dev/production database may be better, because it is closer to real environment.
How it can be done right in Play framework?
Tried:
Map<String, String> settings = new HashMap<String, String>();
settings.put("db.default.url", "jdbc:mysql://localhost/sometestdatabase");
settings.put("db.default.jndiName", "DefaultDS");
app = Helpers.fakeApplication(settings);
Looks like evolutions work here, but how it's best to clean database before each test? By creating custom code that truncates each table? If it will drop tables, then will evolutions run again before next test, or they are applied once per play test
command? Or once per Helpers.fakeApplication()
invocation?
What are best practices here? Heard about dbunit, is it possible to integrate it without much pain and quirks?