Testing Android App using greendao with Mockito


I am new to android testing and trying to write unit tests (running on local jvm) using mockito in Android Studio.

My IDE setup (gradle scripts) is done so far.

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')

   // Unit testing dependencies.
   testCompile 'junit:junit:4.12'
   testCompile 'org.mockito:mockito-core:1.10.19'

In my app I am using greendao as ORM but have no abstraction of this layer yet (planned for future). When I try to test parts of my application code which are using database related classes (like SqliteOpenHelper, SqliteDatabase for setting up database, SQLiteStatement for compiling statements, etc.) the test exits with exception, for example with SQLiteOpenHelper not mocked .

Is it at all possible to write unit tests in this scenario mocking the database (without investing now time to abstract the database layer)?


The problem was to get the right Context for initializing the database layer. Using robolectric you can use RuntimeEnvironment.application to get a Context object in the test environment, so initializing the database layer with unit tests are running on the local JVM without any device or emulator.

Answered By – Dokumans

Leave a Comment