Database – Unit Testing DatabaseOpenHelper class in Android

androiddatabaseunit testing

I've written several unit tests for an already existing DatabaseOpenHelper class. I covered the creation scenario and migration from old schema to new one. Unfortunately, I don't feel that my tests are good. For example, for DB creation I check that SQLiteDatabase. execSQL() was called three times (we have three tables so far) and check that query string is in set of specified SQL strings.

The issue so far – if someone changes order of columns the test will fail. Probably this is OK.

But what are other ways of SQL schema unit testing? Or don't you have unit tests for that, and rely only on the integration one?

Best Answer

Instead of verifying that

  • colum1 is name,
  • colum2 is birthday,
  • colum3 is ....

i would just test if these colums exist after upgrade.

To do this I would create an integration test that

  • starts with a databasefile which has old database version,
  • run the updater and then
  • execute a sql-select for every table with a all fields.

Example:

  • select name, birthday, telefonnumber from customer
  • select orderid, orderdate, customerid from order

Test-Success means there is no database-exception for unknown table or field.

[Update 2.2.2013] Unittesting the DatabaseOpenHelper (Unittest=Testing in isolation) would requre to test without a real database and verifying that the database update script is valid or at least contain all neccessary fields/tables. In my opinion this is to much work. Integration testing with a real database is much easier.