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