5

I am using robolectric as my test runner with Mockito. I would like to know in Android how do i mock a resource. 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.

j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

5

I found a way to do this using 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);
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • 6
    how to create Resource object in test class? I've used MockResources class but giving me null values. – chetan Jan 22 '18 at 04:36