19

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.

public class AddClassEvent extends Activity{
 private String eventType;

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

  Bundle extras = getIntent().getExtras();
  final String cNo = extras.getString("CourseNum");

  // create a model instance
  final StudentDBModel model = new StudentDBModel(this);

  setContentView(R.layout.add_class_event);
 .....
 .....
         }
     }

The test class looks like...

public class AddClassEventTest extends ActivityInstrumentationTestCase2<AddClassEvent>{
 private StudentDBModel model = null;
 private RenamingDelegatingContext context = null;

 public AddClassEventTest() {
  super("com.UI", AddClassEvent.class);
 }

 /**
  * This method is called before each test.
  */
 @Override
 public void setUp() {
  context = new RenamingDelegatingContext(getActivity(), "test_");
  model = new StudentDBModel(context);
 }

 /*
  * This function will test addNewClassEvent() from StudentDBModel
  */
 public void testAddNewClassEvent(){

  ContentValues courseValues = new ContentValues();
  courseValues.put("CourseId", "60-415");
  courseValues.put("CourseName", "Advanced Database Design");
  courseValues.put("Section", "1");
  courseValues.put("Location", "Erie");
  courseValues.put("Credit", "3");
  courseValues.put("ProfEmail", "[email protected]");
  courseValues.put("Website", "cs.uwindsor.ca");

  model.addNewCourses(courseValues);

  int numEventsBefore = model.getNumClassEvents();

  ContentValues values = new ContentValues();

  values.put("EventName", "Assignment 1");
  values.put("CourseId", "60-415");
  values.put("EventType", "Assignment");
  values.put("EventWeight", "8");
  values.put("DueDate", "10/20/2010");

  model.addNewClassEvent(values);

  int numEventsAfter = model.getNumClassEvents();

  assertEquals(numEventsBefore + 1, numEventsAfter);
 }
}

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. Whenever I run the test I get a NULL Pointer Exception on the on the line:

final String cNo = extras.getString("CourseNum");

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?


OK so I have tried to take your advice and I have changed my setUp function to:

@Override
public void setUp() {
    context = new RenamingDelegatingContext(getActivity(), "test_");
    model = new StudentDBModel(context);
    Intent addEvent = new Intent();
    addEvent.setClassName("com.UI", "com.UI.AddClassEvent");
    addEvent.putExtra("CourseNum", "60-415");
    setActivityIntent(addEvent);
    getActivity();
}

but I am still getting a NULL Pointer exception. Is my syntax wrong? Any suggestions?

1
  • 1
    When you use putExtra() you need to include a package prefix with the name. Ex: addEvent.putExtra("com.ui.CourseNum", "60-415");
    – McStretch
    Dec 1, 2010 at 20:50

2 Answers 2

18

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.

2
  • OK, thanks for the reply. I will try this and post my results. Dec 1, 2010 at 18:57
  • 1
    I tried your advice, but still getting the Null Pointer exception. I have added the code for my changes to my setUp function above. Dec 1, 2010 at 20:42
12

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.

1
  • A side question... How does that renamingDelegate context affect the activity itself ? As I see it you only have a different context for the model, not for the activity.
    – user458577
    Mar 26, 2013 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.