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


i writing android junit test class relies on extras passed through intent. able class working properly, still know how write unit test such class, 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 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);  }   /**   * method called before each test.   */  @override  public void setup() {   context = new renamingdelegatingcontext(getactivity(), "test_");   model = new studentdbmodel(context);  }   /*   * function test addnewclassevent() 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);  } } 

the problem is, passing class addclassevent pk db created in class , passed addclassevent through intent. whenever run test null pointer exception on on line:

final string cno = extras.getstring("coursenum"); 

how create info in junit test? there way test work? have searched extensively , can't find answer. there way falsely create extras in junit test thinks being created other class? if so, please show me how?


ok have tried take advice , have changed 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 still getting null pointer exception. syntax wrong? suggestions?

the class inherit, activityinstrumentationtestcase2, allows mock intents. documentation:

you can inject custom intents activity (see setactivityintent(intent)).

the documentation setactivityintent() further clarifies:

call method before first call getactivity() inject customized intent activity under test.

if not call this, default intent provided. if call after activity has been started, have no effect.

so should able place call method inside setup() before call getactivity(). can pass in mocked intent setactivityintent mentioned -- build fake intent extras you'd expect activity see.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -