Q:

Android app testing - How to test DAO layer with real database?

I'm trying to write unit tests for our app and I'm a little bit confused about testing our DAO layer. This layer uses a lot of methods of real SQLiteDatabase and we don't think that using Mockito in this case is a proper way.
So, our question is - how to test DAO layer with real database? Is it real in Android to get real instance of database inside unit tests? 
What we want to test:

execution of SQL statements, for example database.execSQL(..) methods.
execution of insert, update, delete,  rawQuery methods of SQLiteDatabase class.

Thanks in advance!

A:

Sorry, you can't write unittests with real database. These are no unittests by definition.
However, sometimes it can be valuable to provide Fake implementations, e.g. in your case an InMemoryDatabase.
This helps to decouple your code from DB a little bit, because you will have to define an abstract interface and some ways to switch from realDB to fakeDB.
If you're using only a small number of DB methods it may be worth the effort and outweight the Mock solution in your unittests.
However, on the long run, the better way is to improve your design. Please read about "Dependency Inversion Principle" and "Onion Architecture". 

