Android: Modifying the SQLite DB in an Espresso test
Jun 10, 2017
Sometimes you want to modify the db of the app, and then check the UI based on how it displays the items from the db.
Here are some snippets of code to use in your test class to modify your db.
Firstly - it’s important your Activity doesn’t launch automatically at the start of each test. This is because we (well, I) want to modify the db in the test, and the Activity will get confused if you modify the db from under its feet.
We can stop the activity launching automatically by setting the last param in the constructor false as below:
Next, here is an example of a function to clear the database.
Similarly, to insert data:
If you want to use this in a test, you need to make your db changes first, and then call launchActivity(null) on your test rule: