Junit 安卓机器人分子的。测试AccountManager结果

Junit 安卓机器人分子的。测试AccountManager结果,junit,mockito,robolectric,android-testing,Junit,Mockito,Robolectric,Android Testing,在我的应用程序中,我使用帐户经理的方法来获取所有者电子邮件。我如何用Robolectric测试这个方法?我应该为此使用嘲弄吗?如果我是对的,我能用Mockito吗?有什么教程可以教我怎么做吗?首先,我实现了单元测试 // Imports are skipped /** * Created by fminatchy on 25/02/14. */ @RunWith(RobolectricTestRunner.class) @Config(manifest = "/src/main/Androi

在我的应用程序中,我使用帐户经理的方法来获取所有者电子邮件。我如何用Robolectric测试这个方法?我应该为此使用嘲弄吗?如果我是对的,我能用Mockito吗?有什么教程可以教我怎么做吗?

首先,我实现了单元测试

// 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);
    }
其代码位于活动中:

  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;
    }
公共字符串[]getGoogleAccounts(){
最终AccountManager=AccountManager.get(this.getApplicationContext());
Account[]accounts=accountManager.getAccountsByType(GoogleAccountManager.Account\U类型);
字符串[]名称=新字符串[accounts.length];
for(int i=0;i