Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Updated: 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

java.lang.Object     android.content.Context         android.app.ApplicationContext             android.app.Application 

But Google's own Android Javadoc refers to this:

java.lang.Object     android.content.Context         android.content.ContextWrapper             android.app.Application 

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

Update 3:

I have replaced the following line of code:

this.getInstrumentation().getTargetContext(); 

with this line of code.

this.getInstrumentation().getTargetContext().getApplicationContext(); 

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

like image 797
Salsero69 Avatar asked Apr 04 '11 20:04

Salsero69


People also ask

How to get application context in unit Test?

@RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun testContext() { val appContext = InstrumentationRegistry. getInstrumentation(). targetContext assertEquals("com.

What are instrumented tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.


2 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(); 
like image 176
Salsero69 Avatar answered Sep 18 '22 21:09

Salsero69


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                 .getTargetContext()                 .getApplicationContext(); 
like image 33
friederbluemle Avatar answered Sep 18 '22 21:09

friederbluemle