Android ActivityInstrumentationTestCase2 NullPointerException when getting context

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.

+1
android android testing
source share
2 answers

In fact, I had to solve two problems. Firstly, I had an external library included in my main project. When starting the tests, the external library was not properly added. When creating main, it was silent when trying to use the library. To solve this, I followed various solutions here:

Unable to create and run Android test project created using ant create test-project "when the test project has banks in the libs directory

What led me to this:

Android unit test using ant with library project

Android unit test using ant with R18 library project

However, my fix was slightly different. Instead of using the overridden -compile tag in these projects, I just copied the build.xml assembly in my / tools / ant into my test build.xml file and added

  <classpath> <!-- steff: we changed one line here !--> <fileset dir="${tested.android.library.reference.1}/bin/" includes="*.jar"/> <fileset dir="${extensible.libs.classpath}" includes="*.jar" /> </classpath> 

In the same place as the second answer.

The second problem was that the SD card that the emulator was trying to use did not work. I'm not sure exactly for this reason, but creating a new one seems to have solved the problem.

 <sdk>/tools/mksdcard <size> <path> <sdk>/emulator -avd <name> -sdcard <path.from.above> 
0
source share

I think this is what you are looking for:

 public void testExternalFilesDir() { Activity activity = getActivity(); assertNotNull(activity); File file = activity.getExternalFilesDir(null); assertNotNull("No external storage available", file); } 
+1
source share

All Articles