0

I want to run some test adding and getting accounts using AccountManager but I want to do the operations in a new context without the account I already have in the emulator.

Is it possible to do that?

Example of my class

@RunWith(AndroidJUnit4::class)
class AccountTest {

    private val ACCOUNT_TYPE = "com.android.account"

    private lateinit var accountManager: AccountManager

    @Before
    fun init() {
        accountManager = AccountManager.get(ApplicationProvider.getApplicationContext())
    }

    @Test
    fun addAccountTest(){
        val account = Account("test", ACCOUNT_TYPE)
        val result = accountManager.addAccountExplicitly(account, null, null)

        assertThat(result, `is`(true))
    }

    @Test
    fun getAccountTest() {
        val accountList = accountManager.getAccountsByType(ACCOUNT_TYPE).toList()

        assertThat(accountList.size, `is`(0))
    }
}
4

1 回答 1

0

我使用 Robolectric 解决了这个问题 :)

与传统的基于模拟器的 Android 测试不同,Robolectric 测试在沙箱中运行,允许将 Android 环境精确配置为每个测试所需的条件,将每个测试与其邻居隔离,并使用提供分钟的测试 API 扩展 Android 框架控制 Android 框架的行为和断言状态的可见性。

robolectric.org

于 2019-04-01T11:05:04.707 回答