java - Unit Testing a function that takes ActionEvent as a parameter -


how can test function takes "actionevent" type parameter in java/swing? following function want test:

public void actionperformed(actionevent e)     {                             // communicating model class //          /** if "add" button has been pressed **/     if (e.getactioncommand() == "add")     {         string cust_name = view.getaddcustnamefield().gettext();         int ph_num = integer.parseint(view.getaddphonenumfield().gettext());         model.addentry(cust_name, ph_num, "controller");     }      /** if "delete" button has been pressed **/     else if (e.getactioncommand() == "delete")     {         string cust_name = view.getdelcustnamefield().gettext();         model.deleteentry(cust_name, "controller");     }      /** if "update" button has been pressed **/     else if (e.getactioncommand() == "update")     {         string cust_name = view.getupdcustnamefield().gettext();         int ph_num = integer.parseint(view.getupdphonenumfield().gettext());         model.updateentry(cust_name, ph_num, "controller");     }                          // communicating view class //          else      {          /** if "change family" button has been pressed **/         font fontres = view.getresdisplayfield().getfont();         font fontdir = view.getdirdisplayfield().getfont();         if (e.getactioncommand() == "change family")         {             string fontfamily = view.getfontfamilyfield().gettext();                 if (fontfamily != "")             {                 view.getresdisplayfield().setfont(new font(fontfamily, fontdir.getstyle(), fontres.getsize()));             }         }          /** if "change size" button has been pressed **/         else if (e.getactioncommand() == "change size")         {             int fontweight = integer.parseint(view.getfontsizefield().gettext());                            if (fontweight > 0)             {                 view.getresdisplayfield().setfont(new font(fontres.getfamily(), fontres.getstyle(), fontweight));                 view.getdirdisplayfield().setfont(new font(fontdir.getfamily(), fontdir.getstyle(), fontweight));             }         }          /** if "change italics" button has been pressed **/         else if (e.getactioncommand() == "change style")         {             string fontstyle = (string)(view.getfontstylecombo().getselecteditem());             if (fontstyle == "plain")             {                 view.getresdisplayfield().setfont(new font(fontres.getfamily(), font.plain, fontres.getsize()));                 view.getdirdisplayfield().setfont(new font(fontdir.getfamily(), font.plain, fontdir.getsize()));             }              else if (fontstyle == "italic")             {                 view.getresdisplayfield().setfont(new font(fontres.getfamily(), font.italic, fontres.getsize()));                 view.getdirdisplayfield().setfont(new font(fontdir.getfamily(), font.italic, fontdir.getsize()));             }              else if (fontstyle == "bold")             {                 view.getresdisplayfield().setfont(new font(fontres.getfamily(), font.bold, fontres.getsize()));                 view.getdirdisplayfield().setfont(new font(fontdir.getfamily(), font.bold, fontdir.getsize()));             }         }      }// 'else' (if command meant view class) 

so, want emulate button pressing actions , depending on particular button pressed 1 of branches of condition statement executed. corresponding every button there set of 2 jtextfields value have somehow set in unit test function. "...custnamefield" , "...phonenumfield"s value inputting in above function , have set values beforehand can directly input them. how can so?

you can use mocking framework easymock or mokito.

these frameworks allos to:

  • create mock object interface
  • record expected method calls dynamically
  • put mock object in playback mode and
  • pass method tested
  • verify calls made expected.

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -