Home » Android » android – Accessing application context from TestSuite in Setup() before calling getActivity()

android – Accessing application context from TestSuite in Setup() before calling getActivity()

Posted by: admin April 23, 2020 Leave a comment


I have an Activity that pulls an object from an Application extended class (application context) from within the OnCreate() method.

When unit testing this activity, the object needed isn’t there because it is populated from a previous Activity and stored in the above mentioned application context.

Needless to say, when I call getActivity() from within my ActivityInstrumentationTestCase2 extended test case I get a null pointer exception.

How can I populate the context before an activity is started and have it available to that Activity?

After a bit of digging I found: this.getInstrumentation().getTargetContext() and then cast it to the type of my Application extended class. But I get a class cast exception and the trace points to this:

04-04 21:02:27.036: INFO/TestRunner(431): started: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): failed: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): ----- begin exception -----
04-04 21:02:27.136: INFO/TestRunner(431): java.lang.ClassCastException: android.app.ApplicationContext
04-04 21:02:27.136: INFO/TestRunner(431):     at edu.rockies.rockies.activity.courses.test.TopicTest.setUp(TopicTest.java:27)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.runBare(TestCase.java:125)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult$1.protect(TestResult.java:106)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.runProtected(TestResult.java:124)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.run(TestResult.java:109)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.run(TestCase.java:118)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
04-04 21:02:27.136: INFO/TestRunner(431): ----- end exception -----
04-04 21:02:27.156: INFO/TestRunner(431): finished: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)

this.getInstrumentation().getTargetContext() is supposed to return an object of type context. But I get the android.app.ApplicationContext class cast exeption which doesn’t make sense.

Update 2:

I did some more research and discovered this for android.app.Application


But Google’s own Android Javadoc refers to this:


What’s going on? Something’s not right.

Update 3:

I have replaced the following line of code:


with this line of code.


Although the context resolves properly, it doesn’t seem to be the same context as the activity’s.

How to&Answers:

Ok, this issue is resolved. To get access to the context before getActivity() has been called you need to call this function:

Context context = this.getInstrumentation().getTargetContext().getApplicationContext();


With the newer UI testing framework getInstrumentation() is no longer available. One way to get hold of the Application object is to cast the application Context:

Application app = 
        (Application) InstrumentationRegistry


Always use this.getInstrumentation().getTargetContext() to access the context of the application.

is not same as this.getInstrumentation().getTargetContext()

I was running the automation in 4.0.X versions and most of the time getApplicationContext() was returning null context.


You can call the static method for the Context:

Context context = InstrumentationRegistry.getTargetContext();

More details can be found here:


You can get the application context using the androidx.test.core.app.ApplicationProvider.getApplicationContext(). Cast that to your application and it should be the right context.