Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing application context from TestSuite in Setup() before calling getActivity()
    primarykey
    data
    text
    <p>I have an <code>Activity</code> that pulls an object from an <code>Application</code> extended class (application context) from within the <code>OnCreate()</code> method.</p> <p>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.</p> <p>Needless to say, when I call <code>getActivity()</code> from within my <code>ActivityInstrumentationTestCase2</code> extended test case I get a null pointer exception.</p> <p>How can I populate the context before an activity is started and have it available to that <code>Activity</code>?</p> <p><strong>Updated:</strong> After a bit of digging I found: <code>this.getInstrumentation().getTargetContext()</code> and then cast it to the type of my <code>Application</code> extended class. But I get a class cast exception and the trace points to this:</p> <pre><code>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) </code></pre> <p><code>this.getInstrumentation().getTargetContext()</code> is supposed to return an object of type context. But I get the <code>android.app.ApplicationContext</code> class cast exeption which doesn't make sense.</p> <p><strong>Update 2:</strong></p> <p>I did some more research and discovered this for <a href="http://www.androidjavadoc.com/m3-rc37a/index.html?android/app/Application.html" rel="noreferrer">android.app.Application</a></p> <pre><code>java.lang.Object android.content.Context android.app.ApplicationContext android.app.Application </code></pre> <p>But Google's own Android Javadoc refers to <a href="http://developer.android.com/reference/android/app/Application.html" rel="noreferrer">this</a>:</p> <pre><code>java.lang.Object android.content.Context android.content.ContextWrapper android.app.Application </code></pre> <p>What's going on? Something's not right.</p> <p><strong>Update 3:</strong></p> <p>I have replaced the following line of code:</p> <pre><code>this.getInstrumentation().getTargetContext(); </code></pre> <p>with this line of code. </p> <pre><code>this.getInstrumentation().getTargetContext().getApplicationContext(); </code></pre> <p>Although the context resolves properly, it doesn't seem to be the same context as the activity's.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload