1

I am trying to figure out how to use RenamingDelegatingContext to mock a database for testing purposes. The tutorials I have found about this class are great but don't quite answer my questions.

  • dtmilano discusses mocking a file with RenamingDelegatingContext.
  • jayway gives a tutorial about mocking a ContentResolver, but only focus on openInputStream() rather than query() and the other database-like operations

As far as I can tell, there isn't a tutorial about mocking databases. If I am missing it in my googling, please provide a link. Otherwise, this would be an excellent home for such a tutorial.

Update:

Testing database on Android: ProviderTestCase2 or RenamingDelegatingContext? is close to what I am trying to do. The difference is that I have already written tests for my ContentProvider using ProviderTestCase2. Now I am doing some functional testing with ActivityInstrumentationTestCase2 subclasses for some activities which use that ContentProvider. I need the instrumentation for menu and button clicks. If I create a mock context with

RenamingDelegatingContext context =
    new RenamingDelegatingContext(getTargetContext(), TEST_FILE_PREFIX);

how and when do I inject it into the instrumentation?

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Wouldn't it be better to use an actual database without mocking it? No mocking service will be able to mimick a database to a sufficient level to guarantee that passing your test means it will actually work in the database. Why not isolate the logic and db access code so you can test the code and use a transaction roll back or a similar mechanism to get a clean DB environment to test? – Thihara Feb 21 '14 at 04:49
  • @Thihara My app is open source. What if a user wants to run the test suite without disrupting actual "production" data on their device. Currently my tests use the exact same database. My understanding is that `RenamingDelegatingContext` will add a prefix to the database name and thus use a "real" database but a completely different one from the one used by the actual app. I'm just confused about how to set it up to do so. – Code-Apprentice Feb 21 '14 at 05:01
  • Did you see this? http://stackoverflow.com/questions/3096378/testing-database-on-android-providertestcase2-or-renamingdelegatingcontext May be it will help. – Thihara Feb 21 '14 at 05:04
  • @Thihara Yes, I have seen that Q&A as well. I'll take a closer look to see if I can get anything new from it. – Code-Apprentice Feb 21 '14 at 05:38
  • @Thihara Updated my question, if you'll have a look. – Code-Apprentice Feb 21 '14 at 06:00
  • @Code-Apprentice Did you find a solution to this? – alfoks Jul 07 '15 at 14:14
  • @alfoks No, I still have not found a satisfactory solution. – Code-Apprentice Jan 20 '16 at 19:58
  • You've tried playing with Mockito? – IgorGanapolsky Mar 24 '16 at 16:47
  • @IgorGanapolsky I haven't looked at Mockito yet. It is on my "To Do" list. – Code-Apprentice Aug 02 '16 at 18:49

0 Answers0