Reputation: 83527
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?
Upvotes: 2
Views: 3039
Reputation: 83527
The solution which works for me, although not entirely satisfactory, is to replace the call to super.getAllTests()
with new TestSuite()
.
Upvotes: 1