Previously, I wrote tests for an Activity which accesses a database. When a test method finished, I deleted the database file in tearDown()
to restore the device to a known state. This worked fine when the Activity queried the database directly. However, when I implemented a ContentProvider
, I quickly ran into problems because the ContentProvider
keeps the database open. Deleting the file didn't reset the data for the tests because the ContentProvider
continued to query the original database. The work around that I found was to delete rows from the table in my database. This is less than ideal since it still leaves the database in a dirty state (e.g. any autoincrement fields are not reset to start at 0).
More recently, I started looking into closing the database in the ContentProvider
. I learned that the ContentProvider
normally takes care of this for you. This makes sense in the normal execution of an app. However, testing is a different story. In another answer, CommonsWare suggest calling shutdown()
from a unit test. However, my problem is that in most of my tests I don't have a reference to the ContentProvider
. My app uses a ContentResolver
to find the ContentProvider
and the test code itself doesn't have a reference to either of these objects.
With all that said, what is the best way to ensure that a SQLite database used by my app is returned to a clean state after each test finishes?
Aucun commentaire:
Enregistrer un commentaire