正在测试的Robotium活动需要一个Intent

时间:2012-11-24 18:07:36

标签: android testing robotium

我将活动声明为

public class QuestionActivity {

    private Quiz quiz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.question);
        quiz = (Quiz) getIntent().getSerializableExtra("quiz");
        quiz.getQuestions()
    }
}

正如您所看到的,这需要在启动活动的活动所在的意图中有一个Quiz对象。

这是我的单元测试

public class QuestionActivityTest extends
    ActivityInstrumentationTestCase2<QuestionActivity>
{
    public QuestionActivityTest()
    {
        super(QuestionActivity.class);
    }

    public void testExecuteQuiz()
    {
      //Test stuff
    }
}

这个简单的测试什么都不会失败,因为当Robotium创建我的Activity实例时,我会得到一个空指针,因为我看不到如何将我的Quiz实例注入Intent,因此它可用于活动onCreate方法

这是logcat:

 11-24 20:57:24.691: I/TestRunner(1331): started: testExecuteQuiz(com.nquizitive.timestables.QuestionActivityTest)
 11-24 20:57:24.791: I/TestRunner(1331): finished: testExecuteQuiz(com.nquizitive.timestables.QuestionActivityTest)
 11-24 20:57:24.791: I/TestRunner(1331): passed: testExecuteQuiz(com.nquizitive.timestables.QuestionActivityTest)
 11-24 20:57:26.281: I/TestRunner(1346): started: testExecuteQuiz(com.nquizitive.timestables.QuestionActivityTest)
 11-24 20:57:26.641: D/AndroidRuntime(1346): Shutting down VM
 11-24 20:57:26.671: W/dalvikvm(1346): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
 11-24 20:57:26.711: E/AndroidRuntime(1346): FATAL EXCEPTION: main
 11-24 20:57:26.711: E/AndroidRuntime(1346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nquizitive.timestables/com.nquizitive.timestables.QuestionActivity}: java.lang.NullPointerException
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.os.Handler.dispatchMessage(Handler.java:99)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.os.Looper.loop(Looper.java:123)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread.main(ActivityThread.java:4627)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at java.lang.reflect.Method.invokeNative(Native Method)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at java.lang.reflect.Method.invoke(Method.java:521)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at dalvik.system.NativeStart.main(Native Method)
 11-24 20:57:26.711: E/AndroidRuntime(1346): Caused by: java.lang.NullPointerException
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at com.nquizitive.timestables.QuestionActivity.populateQuestion(QuestionActivity.java:128)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at com.nquizitive.timestables.QuestionActivity.populateActivity(QuestionActivity.java:95)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at com.nquizitive.timestables.QuestionActivity.onCreate(QuestionActivity.java:87)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 11-24 20:57:26.711: E/AndroidRuntime(1346):    ... 11 more

1 个答案:

答案 0 :(得分:0)

您可以在执行特定测试之前制作intent并在setUp()中设置。 类似的东西:

protected void setUp() {
     Quiz quiz = new Quiz();
     Intent intent = new Intent();
     intent.putExtra("quiz",quiz);
     setActivityIntent(intent);
}

然后执行测试。