安卓。机器人电子。测试 AccountManager 结果

Nik*_*Lab 5 junit mockito robolectric android-testing

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

fre*_*ric 0

首先我实现了单元测试

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

他们的代码位于活动中:

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