Just started creating UTs for my app and using PowerMock, Mockito and Junit4. Im having a very fundamental problem of mocking Context and getApplicationContext(). I cant seem to understand how to do that.
What i tried so far is the following :
@Before
public void setUp() throws Exception {
mContext = Mockito.mock(Context.class);
Mockito.when(mContext.getApplicationContext()).thenReturn(mContext);
PowerMockito.whenNew(IabHelper.class).withAnyArguments().thenReturn(new IabHelperMock(mContext, null));
}
Everything runs ok when im using debugger (Android Studio) and pausing over the below line, but when i run the test without debugger the test fails (because of the same line) :
public IabHelper(Context ctx, String base64PublicKey) {
mContext = ctx.getApplicationContext();
...
}
The failure message im getting is :
org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl.expectSubstitutionLogic(MockitoNewInvocationControl.java:65)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at com.xxx.libs.components.purchase.util.IabHelper.(IabHelper.java:164)
at com.xxx.libs.mocks.IabHelperMock.(IabHelperMock.java:22)
...
Im clearly doing something wrong and i would like to know what exactly. What is the best practice for using Context and getApplicationContext() ? Thanks.
Aucun commentaire:
Enregistrer un commentaire