android - Activity replace object with mock -
does has solution, how replace objects in activities mocks? test logic in ondestroy(), hard, since can't inject mocks activity.
i came following solution, it's code smell:
activity's code
public handler gethandler() { if (istestcalling()) { return logouthandler; } return null; } public void sethandler(handler logouthandler) { if (istestcalling()) { this.logouthandler = logouthandler; } } private boolean istestcalling() { return util.istestcalling(); } (util class' code somewhere else:)
public static boolean istestcalling() { stacktraceelement[] stacktrace = new exception().getstacktrace(); if (stacktrace.length >= 4) { return (stacktrace[3].getclassname()).equalsignorecase("com.mypackage.activittestclass"); } return false; } test code:
@mock private handler handler; private myactivity activity; @before public void init() { mockitoannotations.initmocks(this); activity = activitycontroller.of(robolectric.getshadowsadapter(), new myactivity()).get(); } @test public void testondestroy() { activity.oncreate(null); activity.sethandler(handler); activity.ondestroy(); verify(handler).removecallbacks(anyobject()); } so, can see able replace object (which initialized in oncreate() ) doing workaround, don't solution, since violating encapsulation , code smell.
any solution this?
Comments
Post a Comment