Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...