title: Adding Android JUnit tests with a custom InstrumentationTestRunner
body: <p>I want to add <a href="https://stackoverflow.com/questions/15017784/parameterized-junit-tests-in-android-test-project">parameterized tests to my Android app test suite</a>. In an answer to my previous question, @dtmilano suggested that I implement a test runner which can pass the arguments to my parameterized tests. I would like to pass these to the test's constructor rather directly rather than <a href="https://stackoverflow.com/questions/14820175/how-to-pass-an-argument-to-a-android-junit-test-parameterized-tests/14821971#14821971">retrieving them via getter methods</a>. As a simple example, I  extended InstrumentationTestRunner:</p>
  
  <pre><code>public class ExampleTestRunner extends InstrumentationTestRunner {
  
      @Override
      public TestSuite getAllTests() {
          TestSuite suite = super.getAllTests();
          suite.addTest(new TestRunnerActivityTest()); // line 22
          return suite;
      }
  }
  </code></pre>
  
  <p>I also update the test project's manifest:</p>
  
  <pre><code>&lt;instrumentation android:name="codeguru.example.testrunner.ExampleTestRunner"
                   android:targetPackage="codeguru.example.testrunner"
                   android:label="Tests for codeguru.example.testrunner"/&gt;
  </code></pre>
  
  <p>When I run this test with</p>
  
  <pre><code>$ adb shell am instrument -w codeguru.example.testrunner.tests/codeguru.example.testrunner.ExampleTestRunner    
  </code></pre>
  
  <p>I get</p>
  
  <pre>E/AndroidRuntime(  880): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{codeguru.example.testrunner.tests/codeguru.example.testrunner.ExampleTestRunner}: java.lang.NullPointerException
  E/AndroidRuntime(  880):        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4145)
  E/AndroidRuntime(  880):        at android.app.ActivityThread.access$1300(ActivityThread.java:130)
  E/AndroidRuntime(  880):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
  E/AndroidRuntime(  880):        at android.os.Handler.dispatchMessage(Handler.java:99)
  E/AndroidRuntime(  880):        at android.os.Looper.loop(Looper.java:137)
  E/AndroidRuntime(  880):        at android.app.ActivityThread.main(ActivityThread.java:4745)
  E/AndroidRuntime(  880):        at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(  880):        at java.lang.reflect.Method.invoke(Method.java:511)
  E/AndroidRuntime(  880):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  E/AndroidRuntime(  880):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  E/AndroidRuntime(  880):        at dalvik.system.NativeStart.main(Native Method)
  E/AndroidRuntime(  880): Caused by: java.lang.NullPointerException
  E/AndroidRuntime(  880):        at codeguru.example.testrunner.ExampleTestRunner.getAllTests(ExampleTestRunner.java:22)
  E/AndroidRuntime(  880):        at android.test.InstrumentationTestRunner.getTestSuite(InstrumentationTestRunner.java:581)
  E/AndroidRuntime(  880):        at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:360)
  E/AndroidRuntime(  880):        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4142)
  E/AndroidRuntime(  880):        ... 10 more</pre>
  
  <p>Clearly, I get an NPE because <code>super.getAllTests()</code> returns <code>null</code>. I can of course just create a <code>new TestSuite()</code> and add my tests to this. Ideally, I want to add my parameterized tests to the rest of the "normal" tests which I already have. Does anyone know of a way I can do this?</p>