I have an ActivityInstrumentationTestCase2, and I was not lucky to get context for the test.
package com.vsnetworks.vsnmedia.test; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.test.ActivityInstrumentationTestCase2; import com.vsnetworks.vsnmedia.MainActivity; import com.vsnetworks.vsnmedia.VSWebViewClient; public class TestMimeTypes extends ActivityInstrumentationTestCase2<MainActivity> { Activity activity; Context context; public TestMimeTypes() { super(MainActivity.class); } public void setUp() throws Exception { super.setUp(); activity = getActivity(); context = activity.getApplicationContext(); } public void test() { String external = context.getExternalFilesDir(null).toString(); }
Here is the error I get:
[exec] com.vsnetworks.vsnmedia.test.TestMimeTypes:INSTRUMENTATION_RESULT: shortMsg=java.lang.NullPointerException [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.NullPointerException: Unable to start activity ComponentInfo{com.vsnetworks.vsnmedia/com.vsnetworks.vsnmedia.MainActivity}: java.lang.NullPointerException [exec] INSTRUMENTATION_CODE: 0
I think this is context related because if I do the following:
Context context = this.getInstrumentation().getTargetContext();
I get (line 37 is context.getExternal line)
[exec] com.vsnetworks.vsnmedia.test.TestMimeTypes: [exec] Error in test: [exec] java.lang.NullPointerException [exec] at com.vsnetworks.vsnmedia.test.TestMimeTypes.test(TestMimeTypes.java:37) [exec] at java.lang.reflect.Method.invokeNative(Native Method) [exec] at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) [exec] at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) [exec] at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) [exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) [exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) [exec] at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) [exec] at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) [exec] [exec] Test results for InstrumentationTestRunner=..E [exec] Time: 0.292 [exec] [exec] FAILURES!!! [exec] Tests run: 2, Failures: 0, Errors: 1
I also tried everything below with the same errors, even with getInstrumentation.waitForIdleSync () (which I saw in another thread) in several places. I tried to create new MainActivity () objects and get the context from there, as well as getActivity (), the same problem.
context = getInstrumentation().getContext(); context = getInstrumentation().getTargetContext(); context = activity.getApplicationContext(); context = activity.getBaseContext();
So what do I see in the world?
Edit 1 - It seems that this problem only occurs on the emulator. If I use a real device to run tests, they both pass.
android android testing
s.field212
source share