Android Mockito 如何模拟资源

j2e*_*nue 5 android mockito robolectric

我使用 robolectric 作为 Mockito 的测试运行器。我想知道在 Android 中如何模拟资源。我有一个数组资源,但在我的测试中,我想模拟该值。

我有一个看起来像这样的数组:

 <array name="screen_sections">
        <item>screen1</item>
        <item>screen2</item>
        <item>screen3</item>
        <item>screen4</item>
    </array>
Run Code Online (Sandbox Code Playgroud)

在测试代​​码中,我想模拟这些值。

j2e*_*nue 5

我找到了一种使用Mockito.when. 所以我会创建一个普通数组并用我的值保存它,这样的东西很好用:

CharSequence myNewArray = {"value1","value2","value3"}
Mockito.when(resources.getTextArray(someID)).thenReturn(myNewArray);
Run Code Online (Sandbox Code Playgroud)

  • 如何在测试类中创建资源对象?我使用了 MockResources 类,但给了我空值。 (6认同)