0

I have an activity declared as

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()
    }
}

As you can see this requires that there be a Quiz object in the intent put there by the activity that launched the activity.

Here is my unit test

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

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

This simple test that does nothing fails because when Robotium creates an instance of my Activity I will get an null pointer because I can't see how do I inject my Quiz instance into an Intent so it will be available for the Activities onCreate method?

Here is the 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
TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
MayoMan
  • 4,757
  • 10
  • 53
  • 85
  • Can you please post the LogCat? – chossen-addict Nov 24 '12 at 18:21
  • added logcat output. I can't really see anything useful in addition to know where the null pointer is thrown. In short, I would expect it to fail as I can't see how to set up the intent in my test before launching the activity that the previous activity sets up in my code when running in real time – MayoMan Nov 24 '12 at 21:01
  • 3
    Just found this other post ( Not sure why I didn't spot it before I posted this question but it pretty much answers my question. http://stackoverflow.com/questions/8335239/android-robotium-test-activity-that-expects-an-extra – MayoMan Nov 24 '12 at 21:47

1 Answers1

0

You can make intent and set in in setUp() before executing the particular test. Something like:

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

And then execute your test.