title: How do I write an android JUnit test when my activity relies on extras passed through an Intent?
body: <p>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.</p>
  
  <pre><code>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);
   .....
   .....
           }
       }
  </code></pre>
  
  <p>The test class looks like...</p>
  
  <pre><code>public class AddClassEventTest extends ActivityInstrumentationTestCase2&lt;AddClassEvent&gt;{
   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", "rfortier@uwindsor.ca");
    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);
   }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>final String cNo = extras.getString("CourseNum");
  </code></pre>
  
  <p>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?</p>
  
  <hr>
  
  <p>OK so I have tried to take your advice and I have changed my setUp function to:</p>
  
  <pre><code>@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();
  }
  </code></pre>
  
  <p>but I am still getting a NULL Pointer exception. Is my syntax wrong? Any suggestions?</p>