android-build-an-app-architecture-components icon indicating copy to clipboard operation
android-build-an-app-architecture-components copied to clipboard

How should we mock the room database and dao's?

Open edwardotis opened this issue 7 years ago • 1 comments

I've found no example code available from google (or anyone) of unit tests that depend on mocking the Room database and tightly coupled dao objects: SunshineDatabase.getInstance(context).weatherDao(); Note: I'm explicitly not talking about instrumented tests.

I used PowerMock(ito) to mock the canonical RoomDatabase.getInstance() static method at the heart of it all. Is this the best way? Or is it skipped because Dagger2 is the preferred way, and that is deemed to complex for these example apps?

Either way, please add best practice examples of unit testing Room with mocks, whether it be with dependency injection or mocking the static getInstance() method.

Thanks.

edwardotis avatar May 12 '18 17:05 edwardotis

I am struggling with the same issue. For how widespread use of Room has become I have not found much for testing in isolation

AshleyHope avatar Sep 20 '18 15:09 AshleyHope