2

I want to add parameterized tests to my Android app test suite. 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 retrieving them via getter methods. As a simple example, I extended InstrumentationTestRunner:

public class ExampleTestRunner extends InstrumentationTestRunner {

    @Override
    public TestSuite getAllTests() {
        TestSuite suite = super.getAllTests();
        suite.addTest(new TestRunnerActivityTest()); // line 22
        return suite;
    }
}

I also update the test project's manifest:

<instrumentation android:name="codeguru.example.testrunner.ExampleTestRunner"
                 android:targetPackage="codeguru.example.testrunner"
                 android:label="Tests for codeguru.example.testrunner"/>

When I run this test with

$ adb shell am instrument -w codeguru.example.testrunner.tests/codeguru.example.testrunner.ExampleTestRunner    

I get

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

Clearly, I get an NPE because super.getAllTests() returns null. I can of course just create a new TestSuite() 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?

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I tried the same, have overridden `getAllTests()`, but when I run it from eclipse with the custom runner, on an empty test class, it just only shows that testclass, not the dynamically generated tests from the runner?? Any insights or ideas on how to make this work? Possibly it's only from commandline that it works? – Peterdk Sep 17 '13 at 18:08
  • When running the command using commandline and adb, like you showed, it does work correctly! Also found out that it works ok in eclipse, if you select in the `run configuration` `run all tests` instead of selecting a single test. – Peterdk Sep 17 '13 at 18:12
  • @Peterdk If you still need help, I suggest that you post your own question with a link to this one. Just be sure to explain why your issue is different than what I asked about here. – Code-Apprentice Sep 17 '13 at 23:02

1 Answers1

1

The solution which works for me, although not entirely satisfactory, is to replace the call to super.getAllTests() with new TestSuite().

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268