Android Robolectric Testing AccountManager Results

In my application, I use a method with an account manager to retrieve the email of the owner. How can I test this method with Robolectric? Should I use mockery for this purpose? If I am right, can I use Mockito? Are there any tutorials how can I do this?

+4
source share
1 answer

First I implemented unit test

// Imports are skipped /** * Created by fminatchy on 25/02/14. */ @RunWith(RobolectricTestRunner.class) @Config(manifest = "/src/main/AndroidManifest.xml") public class TestAuthorization { AccountManager accountManager; Account account0; Account account1; Account account2; @Before public void init() { creationComptes(); accountManager = AccountManager.get(Robolectric.application); shadowOf(accountManager).addAccount(account0); shadowOf(accountManager).addAccount(account1); shadowOf(accountManager).addAccount(account2); } @Test public void test_comptes() { final AlbumsActivity activity = Robolectric.buildActivity(AlbumsActivity.class).create().get(); final String[] accountsName = activity.getGoogleAccounts(); assertThat(Arrays.asList(accountsName)).containsExactly("compte n°1", "compte n°3"); } private void creationComptes() { account0 = new Account("compte n°1", GoogleAccountManager.ACCOUNT_TYPE); account1 = new Account("compte n°2", "pas google"); account2 = new Account("compte n°3", GoogleAccountManager.ACCOUNT_TYPE); } 

and their code is in activity:

  public String[] getGoogleAccounts() { final AccountManager accountManager = AccountManager.get(this.getApplicationContext()); Account[] accounts = accountManager.getAccountsByType(GoogleAccountManager.ACCOUNT_TYPE); String[] names = new String[accounts.length]; for (int i = 0; i < names.length; i++) { names[i] = accounts[i].name; } return names; } 
0
source

All Articles