Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking android.os.Build.MODEL

I am trying to create a test using Mockito/PowerMockito in which we use the value from android.os.Build.MODEL. I've tried using:

@RunWith(PowerMockRunner.class)
@PrepareForTest({android.os.Build.class})
public class Test {
  @Before
  public void init() {
    PowerMockito.spy(android.os.Build.class);
    PowerMockito.doReturn("model").when(Build.class, "getString", "ro.product.model");
  }
}

because I saw that Device.MODEL was making a call to the private static String getString method, but I get the following exception:

org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'getString' with parameter types: [ java.lang.String ] in class android.os.Build.

Is there any way to mock this variable?

like image 759
Guillaume Avatar asked Oct 23 '25 16:10

Guillaume


1 Answers

ReflectionHelpers.setStaticField(Build.class,"MODEL", "Value-You-Want-Model-To-Be");

I like robolectric for that sort of thing, you'd use the above call to mock out the field

like image 129
Ashley Campbell Avatar answered Oct 26 '25 07:10

Ashley Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!