Get context of test project in Android junit test case

AndroidJunit

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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: https://stackoverflow.com/questions/29969545/whats-the-difference-between-gettargetcontext-and-getcontext-on-instrumentat

Solution 2 - Android

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));
	}
}

Solution 3 - Android

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. :)

Solution 4 - Android

import androidx.test.core.app.ApplicationProvider;

    private Context context = ApplicationProvider.getApplicationContext();

Solution 5 - Android

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)

Solution 6 - Android

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

Solution 7 - Android

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

import androidx.test.platform.app.InstrumentationRegistry

InstrumentationRegistry.getInstrumentation().context

Solution 8 - Android

@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

       
        })
    }


}

Solution 9 - Android

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

Solution 10 - Android

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);
    }

Solution 11 - Android

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))
}

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpecepsView Question on Stackoverflow
Solution 1 - Androidfada21View Answer on Stackoverflow
Solution 2 - AndroidAgentKnopfView Answer on Stackoverflow
Solution 3 - AndroidTimothée JeanninView Answer on Stackoverflow
Solution 4 - AndroideoinzyView Answer on Stackoverflow
Solution 5 - AndroidJuanes30View Answer on Stackoverflow
Solution 6 - AndroideslambView Answer on Stackoverflow
Solution 7 - AndroidtomrozbView Answer on Stackoverflow
Solution 8 - AndroidHitesh SahuView Answer on Stackoverflow
Solution 9 - AndroidYoni KerenView Answer on Stackoverflow
Solution 10 - AndroidSebastien FERRANDView Answer on Stackoverflow
Solution 11 - Androidjagadishlakkurcom jagadishlakkView Answer on Stackoverflow