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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With