How do I write an android JUnit test when my activity relies on extras passed through an Intent?

The class you inherit ActivityInstrumentationTestCase2 allows you to mock Intent s. From the documentation : You can inject custom Intents into your Activity (see setActivityIntent(Intent)) The documentation for setActivityIntent() further clarifies: Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test If you do not call this, the default intent will be provided. If you call this after your Activity has been started, it will have no effect So you should be able to place a call to this method inside your setUp() before your call to getActivity() You can pass in a mocked Intent into setActivityIntent like you mentioned -- just build a fake Intent with extras that you'd expect the Activity to see.

The class you inherit, ActivityInstrumentationTestCase2, allows you to mock Intents. From the documentation: You can inject custom Intents into your Activity (see setActivityIntent(Intent)). The documentation for setActivityIntent() further clarifies: Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test.

If you do not call this, the default intent will be provided. If you call this after your Activity has been started, it will have no effect. So you should be able to place a call to this method inside your setUp() before your call to getActivity().

You can pass in a mocked Intent into setActivityIntent like you mentioned -- just build a fake Intent with extras that you'd expect the Activity to see.

OK, thanks for the reply. I will try this and post my results. – Steve Westfall Dec 1 '10 at 18:57 I tried your advice, but still getting the Null Pointer exception.

I have added the code for my changes to my setUp function above. – Steve Westfall Dec 1 '10 at 20:42 This solution works well - thanks! – Peter Sankauskas May 18 at 6:31.

OK, I figured out my mistake! The code for setUp was just in the wrong order. It should look like: @Override public void setUp() { Intent addEvent = new Intent(); addEvent.

SetClassName("com. UI", "com.UI. AddClassEvent"); addEvent.

PutExtra("CourseNum", "60-415"); setActivityIntent(addEvent); context = new RenamingDelegatingContext(getActivity(), "test_"); model = new StudentDBModel(context); } I was calling getActivity() twice and the first call was ahead of the Intent. By using the correct order, the test runs fine. Thanks for the help McStretch.

Please mark either your answer or mine as accepted so the question is closed. – McStretch Dec 2 '10 at 13:31.

I am writing an android Junit test for a class that relies on extras passed to it through an Intent. I was able to get the class working properly, but I would still like to know how to write a unit test for such a class, as the test still fails. The problem is, the extra that I am passing to the class AddClassEvent is a PK for my DB that is created in another class and passed to AddClassEvent through an Intent.

How do I create the info from the extra in the Junit Test? Is there a way to get this test to work? I have searched extensively and can't find an answer.

Is there some way to falsely create the extras in the Junit test so that it thinks it is being created by the other class? If so, could someone please show me how? But I am still getting a NULL Pointer exception.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions