简体   繁体   English

Android Mockito如何模拟资源

[英]Android Mockito how to mock a resource

I am using robolectric as my test runner with Mockito. 我正在使用robolectric作为我与Mockito的测试运行员。 I would like to know in Android how do i mock a resource. 我想在Android中知道如何模拟资源。 I have a array resource but in my test i'd like to mock the value. 我有一个数组资源,但在我的测试中,我想嘲笑价值。

i have an array that looks like this: 我有一个看起来像这样的数组:

 <array name="screen_sections">
        <item>screen1</item>
        <item>screen2</item>
        <item>screen3</item>
        <item>screen4</item>
    </array>

and in test code i'd like to mock these values. 在测试代​​码中,我想模仿这些值。

I found a way to do this using Mockito.when . 我找到了一种方法来使用Mockito.when So I would create a normal array and save it with my values, something like this works good: 所以我会创建一个普通的数组并用我的值保存它,这样的效果很好:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM