java - How do I unit test my MainActivity? -
from understand unit testing more writing code testable writing actual tests.
in order write testable code, in languages java @ least, should using dependency injection right? app i'm writing not use di.
but wanted start writing unit tests mainactivity looks this:
public class mainactivity extends appcompatactivity implements view.onclicklistener{ private imagebutton buttonplay; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //setting orientation landscape setrequestedorientation(activityinfo.screen_orientation_landscape); //prevent screen dimming when app running getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); //getting button , adding click listener buttonplay = (imagebutton) findviewbyid(r.id.buttonplay); buttonplay.setonclicklistener(this); } @override public void onclick(view v) { //starting game activity startactivity(new intent(this, gameactivity.class)); } }
should have test every method here? 1 oncreate(), setcontentview(), setrequestedorientation(), etc? because im not sure asserting methods.
you can use either local unit testing (jvm based) or espresso unit testing or both! in simple terms, local unit testing can test whether calling function (after mocking dependencies if any) results in calling of desired function or not. espresso unit testing can test flow of application. recommend this codelab if want start unit testing in android.
Comments
Post a Comment