Sunday 15 March 2015

android - Using mockito to mock AccountManager -



android - Using mockito to mock AccountManager -

i'm using mockito mock accountmanager within activity test.

so, test code follows:

public class pressureslistactivityunittest extends activityunittestcase<pressureslistactivity> { // test data. private static final string account_type = "com.example.android"; private static final business relationship account_1 = new account("account1@gmail.com", account_type); private static final business relationship account_2 = new account("account2@gmail.com", account_type); private static final account[] two_accounts = { account_1, account_2 }; @mock private accountmanager mmockaccountmanager; public pressureslistactivityunittest() { super(pressureslistactivity.class); } @override protected void setup() throws exception { super.setup(); setupdexmaker(); // initialize mockito. mockitoannotations.initmocks(this); } public void testaccountnotfound() { mockito.when(mmockaccountmanager.getaccounts()) .thenreturn(two_accounts); intent intent = new intent(intent.action_main); startactivity(intent, null, null); } /** * workaround mockito , jb-mr2 incompatibility avoid * java.lang.illegalargumentexception: dexcache == null * * @see <a href="https://code.google.com/p/dexmaker/issues/detail?id=2"> * https://code.google.com/p/dexmaker/issues/detail?id=2</a> */ private void setupdexmaker() { // explicitly set dexmaker cache, tests utilize mockito work final string dexcache = getinstrumentation().gettargetcontext().getcachedir().getpath(); system.setproperty("dexmaker.dexcache", dexcache); }

and oncreate mthod of activity tested:

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_pressures_list); accountmanager = accountmanager.get(this); account[] accounts = am.getaccounts(); if (accounts.length > 0) { log.i("tag", "it works!"); } }

but when run test, accountmanager.getaccounts not homecoming accounts specified in test.

any idea?

that's not how mockito works.

import static org.mockito.mockito.*; ... public void testaccountnotfound() { accountmanager = mock(accountmanager.class); when(am.getaccounts()).thenreturn(two_accounts); // how unit test asserttrue(am.getaccounts() == 2); } public void testmorerealworldexample() { accountmanager = mock(accountmanager.class); when(am.getaccounts()).thenreturn(two_accounts); /* seek , create account; createnewaccount() phone call getaccounts() find out how many accounts there in system, , due above injection, think there two. can test create sure users cannot create 3 or more accounts. */ boolean accountcreated = am.createnewaccount(); // maximum 2 accounts allowed, should homecoming false. assertfalse(accountcreated); }

you're can't straight utilize mockito arbitrarily inject values in objects , run activity. mockito unit testing objects, ideally minimal references android-specific objects, though references inevitable.

please read the cookbook more closely pretty thorough.

if want mock , entire activity, you'll need in robolectric

android mockito

No comments:

Post a Comment