67

Switched to AndroidX and received deprecated: import androidx.test.InstrumentationRegistry.

If I made next import: import androidx.test.platform.app.InstrumentationRegistry I can't use getContext().

Ex: val context = InstrumentationRegistry.getContext().

In build.gradle:

androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
Jonik
  • 80,077
  • 70
  • 264
  • 372
Morozov
  • 4,968
  • 6
  • 39
  • 70

6 Answers6

109

You can use InstrumentationRegistry.getInstrumentation().getTargetContext() in the most cases from androidx.test.platform.app.InstrumentationRegistry.

If you need the Application, you can use ApplicationProvider.getApplicationContext<MyAppClass>().

If you haven't already, I think you can also use the new test dependency: androidTestImplementation 'androidx.test:core:1.0.0-beta02'.

JuanMoreno
  • 2,498
  • 1
  • 25
  • 34
karuto
  • 1,228
  • 1
  • 10
  • 7
54

When you're using Android X you need to make sure you have the following in your app's build.gradle file

androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'

The second one is to make sure you have the correct AndroidJUnit4 to use in your tests.

Make sure you import both of these.

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

Now instead of using val context = InstrumentationRegistry.getContext() you can use the line shown below

val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
  • 6
    I sincerely wonder why android studio projects using androidx don't come out of the box with the testing part up to date. – Dakatine Mar 07 '19 at 11:53
  • 1
    This is giving me "error: package androidx.test.platform.app does not exist" – Phlip Jun 13 '19 at 21:11
  • This is the real trick to solve all of these contexts' version problem for androidX user. – Wesely Jul 05 '19 at 01:43
  • @Mark O'Sullivan, I'm receiving the runtime error '_Unresolved reference: test_' on this line - `import androidx.test.platform.app.InstrumentationRegistry`. This is within the **test** directory for a JUnit test. – AdamHurwitz Aug 11 '19 at 22:43
  • 1
    Here is the [solution](https://stackoverflow.com/a/57454390/2253682) to my issue above with the **import**. It was related to how the library dependency is added. – AdamHurwitz Aug 12 '19 at 00:16
  • 1
    @Adam Hurwitz suggestion works to me. Thanks a lot. – Ajay Oct 11 '19 at 12:02
  • So for anyone new to Android: make sure your test class is in the ANDROID test package, not the regular test package. That was the issue for me. – The Fox Nov 09 '19 at 07:51
  • 1
    @Mark O'Sullivan this worked like magic for me Thanks ! – ibrhm117 Jun 16 '20 at 11:34
12

The following code is deprecated now:

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

Instead use:

Context context = ApplicationProvider.getApplicationContext();
Elletlar
  • 3,136
  • 7
  • 32
  • 38
Akgun Studio
  • 166
  • 1
  • 7
8

I spent a lot of time moving dependency with testImplementation instead of androidTestImplementation and reverse, in the app build.gradle.

My fault was that I have created the test class in the test folder instead of androidTest folder so getting unresolved error for AndroidJuit4 and InstrumentationRegistry.

When I shifted my test file to the androidTest folder then issue been solved with depandency implementation of test libraries with androidTestImplementation in the build.gradle.

Ajay
  • 1,255
  • 1
  • 17
  • 30
  • I don't know why you don't get more upvotes. This was the issue for me. In hindsight it's obvious, but when first doing Android testing it isn't obvious that the folder matters a lot. – The Fox Nov 09 '19 at 07:50
4

For Kotlin usage, in order to get Context:

InstrumentationRegistry.getInstrumentation().targetContext
Ziem
  • 6,579
  • 8
  • 53
  • 86
lomza
  • 9,412
  • 15
  • 70
  • 85
3

Use below import

import androidx.test.platform.app.InstrumentationRegistry

Kotlin ex
   InstrumentationRegistry.getInstrumentation().context,
Sam
  • 6,215
  • 9
  • 71
  • 90