73

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
82.1k77 gold badges272 silver badges386 bronze badges
asked Oct 22, 2018 at 7:39

6 Answers 6

113

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'.

Juan Moreno
2,8151 gold badge29 silver badges39 bronze badges
answered Oct 22, 2018 at 15:00
Sign up to request clarification or add additional context in comments.

4 Comments

It's InstrumentationRegistry from androix.test.platform.app package
Comment from @woprandi fixed my issue. Thanks!
I've added "androidx.test:core:1.1.0" but I don't have access to ApplicationProvider. What do I miss? Thanks
@lorenzo see my answer
57

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()
answered Dec 18, 2018 at 20:00

8 Comments

I sincerely wonder why android studio projects using androidx don't come out of the box with the testing part up to date.
This is giving me "error: package androidx.test.platform.app does not exist"
Here is the solution to my issue above with the import. It was related to how the library dependency is added.
@Adam Hurwitz suggestion works to me. Thanks a lot.
@Mark O'Sullivan this worked like magic for me Thanks !
|
13

The following code is deprecated now:

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

Instead use:

Context context = ApplicationProvider.getApplicationContext();
Elletlar
3,2547 gold badges35 silver badges40 bronze badges
answered May 24, 2019 at 5:27

2 Comments

That gives "error: package androidx.test.core.app does not exist"
try to add androidTestImplementation 'androidx.test:core:1.2.0'
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.

answered Oct 11, 2019 at 11:49

1 Comment

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.
4

For Kotlin usage, in order to get Context:

InstrumentationRegistry.getInstrumentation().targetContext
Ziem
6,7278 gold badges57 silver badges86 bronze badges
answered Mar 12, 2019 at 15:43

Comments

3

Use below import

import androidx.test.platform.app.InstrumentationRegistry
Kotlin ex
 InstrumentationRegistry.getInstrumentation().context,
answered Oct 29, 2019 at 0:41

1 Comment

The import path is also the same for Java with SDK version 30.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.