140

Does anyone know how can you get the context of the Test project in Android junit test case (extends AndroidTestCase).

Note: The test is NOT instrumentation test.

Note 2: I need the context of the test project, not the context of the actual application that is tested.

I need this to load some files from assets from the test project.

peceps
  • 17,370
  • 11
  • 72
  • 79
  • Why can't you just use InstrumentationTestCase? – yorkw Dec 22 '11 at 19:49
  • 2
    Because I am testing services, not UI. – peceps Dec 23 '11 at 16:48
  • 1
    There's a better answer found here: [Using AndroidTestCase instead of a JUnit test][1] [1]: http://stackoverflow.com/questions/3170706/how-do-you-get-hold-of-an-android-context-for-a-junit-test-from-a-java-project – Jaime Botero Aug 10 '12 at 19:23

12 Answers12

184

There's new approach with Android Testing Support Library (currently androidx.test:runner:1.1.1). Kotlin updated example:

class ExampleInstrumentedTest {

    lateinit var instrumentationContext: Context

    @Before
    fun setup() {
        instrumentationContext = InstrumentationRegistry.getInstrumentation().context
    }

    @Test
    fun someTest() {
        TODO()
    }
}

If you want also app context run:

InstrumentationRegistry.getInstrumentation().targetContext

Full running example: https://github.com/fada21/AndroidTestContextExample

Look here: What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?

fada21
  • 3,188
  • 1
  • 22
  • 21
  • 3
    Finally an answer on how to use JUnit4 with InstrumentationTest. After hours of searching. Gotta love Android development. – Fabian Zeindl Jun 14 '16 at 08:33
  • 1
    Nice! tx (note there is a typo in your class member) – Greg Sep 23 '16 at 10:00
  • 1
    Could someone help about what dependencies need to be added in the gradle file in order for this to work? – Greg Sep 23 '16 at 10:09
  • Something must have changed. AndroidStudio can't find "AndroidJUnit4" and "InstrumentationRegistry". – Piotrek Feb 17 '18 at 17:33
  • 1
    Don't forget to add `compile "com.android.support.test:runner:1.0.1"` to your gradle – jujka Mar 21 '18 at 11:43
  • 2
    It's deprecated, use `InstrumentationRegistry.getInstrumentation().context` instead. – Allan Veloso Mar 15 '19 at 13:56
  • @fada21, I'm using `import androidx.test.platform.app.InstrumentationRegistry`, however the runtime error is occurring '_Unresolved reference: test_' on that import within the **test** directory for a JUnit test. – AdamHurwitz Aug 11 '19 at 22:42
  • 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:15
  • For my unit test I needed access to the internal application files with `context.getFilesDir();`. In order to see the files it needs to be `Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();`. – B Porr Feb 15 '21 at 10:01
  • No instrumentation registered! Must run under a registering instrumentation. – Mostafa Imani Sep 26 '22 at 08:02
39

After some research the only working solution seems to be the one yorkw pointed out already. You'd have to extend InstrumentationTestCase and then you can access your test application's context using getInstrumentation().getContext() - here is a brief code snippet using the above suggestions:

public class PrintoutPullParserTest extends InstrumentationTestCase {

    public void testParsing() throws Exception {
        PrintoutPullParser parser = new PrintoutPullParser();
        parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration));
    }
}
AgentKnopf
  • 4,295
  • 7
  • 45
  • 81
  • 7
    Yes but it seems silly that Android does not provide access to the test project context in simple JUnit tests. The context is there in AndroidTestCase.mTestContext but it is private. I don't see why. – peceps Jan 17 '12 at 09:07
  • @peceps Full Ack - but thats just how it is and I dont like it neither ;) – AgentKnopf Jan 19 '12 at 09:34
25

As you can read in the AndroidTestCase source code, the getTestContext() method is hidden.

/**
 * @hide
 */
public Context getTestContext() {
    return mTestContext;
}

You can bypass the @hide annotation using reflection.

Just add the following method in your AndroidTestCase :

/**
 * @return The {@link Context} of the test project.
 */
private Context getTestContext()
{
    try
    {
        Method getTestContext = ServiceTestCase.class.getMethod("getTestContext");
        return (Context) getTestContext.invoke(this);
    }
    catch (final Exception exception)
    {
        exception.printStackTrace();
        return null;
    }
}

Then call getTestContext() any time you want. :)

Timothée Jeannin
  • 9,652
  • 2
  • 56
  • 65
  • 2
    Worked perfectly for me and I load assets using Context either of AndroidTestCase via this method, or ActivityInstrumentationTestCase2.getInstrumentation ().getContext () then getResources ().getAssets () – Andrew Mackenzie Aug 24 '13 at 16:56
  • 1
    Can you speculate as to why they made it hidden? If we use this technique, could they take the method away in a later release (breaking our test code)? – Andrew Shepherd Nov 12 '14 at 23:38
  • 1
    I get `java.lang.NoSuchMethodException: android.test.ServiceTestCase.getTestContext()` – kurdtpage May 23 '18 at 04:28
9

If you want to get the context with Kotlin and Mockito, you can do it in the following way:

val context = mock(Context::class.java)
Juanes30
  • 2,398
  • 2
  • 24
  • 38
8

@RunWith(AndroidJUnit4.class) let you use Android Context

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.android.systemui", appContext.getPackageName());
    }


}

You can even run it on main thread using runOnMainSync. Here is the complete solution:

@RunWith(AndroidJUnit4::class)
class AwesomeViewModelTest {

    @Test
    fun testHandler() {

        getInstrumentation().runOnMainSync(Runnable {
            val context = InstrumentationRegistry.getInstrumentation().targetContext

          // Here you can call methods which have Handler

       
        })
    }


}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
7
import androidx.test.core.app.ApplicationProvider;

    private Context context = ApplicationProvider.getApplicationContext();
eoinzy
  • 2,152
  • 4
  • 36
  • 67
5

Update: AndroidTestCase This class was deprecated in API level 24. Use InstrumentationRegistry instead. New tests should be written using the Android Testing Support Library. Link to announcement

You should extend from AndroidTestCase instead of TestCase.

AndroidTestCase Class Overview
Extend this if you need to access Resources or other things that depend on Activity Context.

AndroidTestCase - Android Developers

Hemant Kaushik
  • 1,706
  • 15
  • 22
eslamb
  • 123
  • 1
  • 8
5

This is to correct way to get the Context. Other methods are already deprecated

import androidx.test.platform.app.InstrumentationRegistry

InstrumentationRegistry.getInstrumentation().context
tomrozb
  • 25,773
  • 31
  • 101
  • 122
1

For Kotlin unit test with @RunWith(AndroidJUnit4::class)

Add this dependency for kotlin test

implementation 'androidx.test:core-ktx:1.5.0'

In the test class access context using the below snippet.

 private val context = ApplicationProvider.getApplicationContext<Context>()
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
0

The other answers are outdated. Right now every time that you extend AndroidTestCase, there is mContext Context object that you can use.

Yoni Keren
  • 1,170
  • 2
  • 13
  • 24
0

For those encountering these problems while creating automated tests, you've gotta do this :

    Context instrumentationContext;

    @Before
    public void method() {

        instrumentationContext = InstrumentationRegistry.getInstrumentation().getContext();

        MultiDex.install(instrumentationContext);
    }
Sebastien FERRAND
  • 2,110
  • 2
  • 30
  • 62
0

Add Mocito Library

testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'org.mockito:mockito-core:3.10.0'

Add Annoatation call @Mock where ever need for example for Context

@RunWith(MockitoJUnitRunner::class)

class EmailValidatorTest {

@Mock
private lateinit var context: Context

lateinit var utils:Utils

@Before
fun launch()
{
    utils=Utils(context)
}

@Test
fun emailValidator_NullEmail_ReturnsFalse() {
    assertFalse(utils.isValidEmail(null))
}

}