8

As per https://stackoverflow.com/a/13556184/3286489, we could use RenamingDelegatingContext for aiding the SQLite DB unit test for Android development.

However in beginning API level 24, android doc announced that this is now deprecated. So what is the new approach of testing we could do in replacing RenamingDelegatingContext?

Community
  • 1
  • 1
Elye
  • 53,639
  • 54
  • 212
  • 474
  • are you running your tests on the device? have you weighed the option of using robolectric for some of your tests that would be better suited for robolectric on the jvm? – TrevJonez Oct 12 '16 at 00:22
  • I have the same question. What little I have been able to research leads me to believe that the preferred approach is to use Mockito. I have not had time to experiment with this yet. – Code-Apprentice Oct 12 '16 at 00:24
  • My preferences is not to have device test. But if that is what required, then I'll go for it. – Elye Oct 12 '16 at 00:31
  • @TrevJonez The Testing Support Library now offers functionality similar to Robolectric for local unit tests and is supported directly in Android Studio. – Code-Apprentice Oct 12 '16 at 16:26
  • @Code-Apprentice it is hardly the same. There are down sides to every approach but I would rather use robolectric and not worry about most mocking situations the way the official docs recommend doing. – TrevJonez Oct 14 '16 at 00:26
  • @TrevJonez I guess I need to look into Robolectric more. The few times I tried it, I didn't have any success. – Code-Apprentice Oct 14 '16 at 00:32
  • @Code-Apprentice, check out my answer below. Using Robolectric successfully perform the needed test. Hope this helps. Cheers. – Elye Oct 14 '16 at 15:13
  • I'd be interested in finding out what a Mockito based solution would look like. – fejd May 12 '17 at 09:08
  • @Code-Apprentice, what is this functionality you mentioned? Provided a link? – Neon Warge Aug 05 '17 at 17:55
  • @NeonWarge I am not sure what you are referring to. Can you provide the exact quote from my comment that you are asking about? – Code-Apprentice Aug 05 '17 at 18:48
  • @Code-Apprentice this one "The Testing Support Library now offers functionality similar to Robolectric for local unit tests and is supported directly in Android Studio" I don't know what you are referring here either. You mentioned it is officially supported and I do not know what was officially supported. Link to the documentation helps. – Neon Warge Aug 05 '17 at 23:44
  • 1
    @NeonWarge I was referring to running unit tests on a JVM on your development machine rather than deploying to an Android device. Prior to Android Studio 2.0, the only solution available for running local unit tests was Robolectric. Since AS 2.0, Google officially supports local unit tests without adding any third-party dependencies. – Code-Apprentice Aug 07 '17 at 03:44

2 Answers2

5

If targetSdkVersion 28, you must add the following to your module's build.gradle:

android {
    ...

    // Gradle automatically adds 'android.test.runner' as a dependency.
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

By doing this, RenamingDelegatingContext and other missing classes will be available again.

Source: https://developer.android.com/training/testing/set-up-project

Tim Kist
  • 1,164
  • 1
  • 14
  • 38
MoGa
  • 635
  • 6
  • 14
  • 1
    Better to take a look at Android Test https://android.github.io/android-test/ and use `InstrumentationRegistry.getTargetContext()` – Tim Kist Sep 26 '18 at 11:59
3

I have found a solution, where we could just use Robolectric's RuntimeEnvironment.application as the Context, and the others would be straight forward as normal Unit test process.

In case if more details is needed, do refer to this, that has complete code of having Android SQLite DB unit tested. https://medium.com/@elye.project/android-sqlite-database-unit-testing-is-easy-a09994701162#.rhdv2qa9o

Elye
  • 53,639
  • 54
  • 212
  • 474