Skip to content

Commit

Permalink
MOBILE-7037: Update mockito dependency (#5)
Browse files Browse the repository at this point in the history
refactoring(Mockito):
- Update mockito version and applied changes for tests
  • Loading branch information
KostiantynTselykh committed Apr 28, 2021
1 parent 138c913 commit 123918b
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion PrebidMobile/PrebidMobile-rendering/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ dependencies {
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'junit:junit:4.12'

testImplementation 'org.mockito:mockito-core:1.10.19'
testImplementation 'org.mockito:mockito-core:2.7.22'

testImplementation 'org.json:json:20180813'
testImplementation 'com.google.code.gson:gson:2.8.6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ public void destroy_StopVisibilityTracker() {

private void mockClickListener(View clickViewToMock, View onClickParam) {
doAnswer(invocation -> {
final View.OnClickListener listener = invocation.getArgumentAt(0, View.OnClickListener.class);
final View.OnClickListener listener = invocation.getArgument(0);
listener.onClick(onClickParam);
return null;
}).when(clickViewToMock).setOnClickListener(any());
}

private void prepareIntentVerifyUrl(Context spyContext, NativeAdLink nativeAdLink) {
doAnswer(invocation -> {
final Intent intent = invocation.getArgumentAt(0, Intent.class);
final Intent intent = invocation.getArgument(0);
final String url = intent.getStringExtra(EXTRA_URL);
assertEquals(nativeAdLink.getUrl(), url);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void MRAIDContinueTest() throws Exception {

private Answer<Object> prepareHandlerAnswer(final String jsonString) {
return invocation -> {
Handler handler = invocation.getArgumentAt(0, Handler.class);
Handler handler = invocation.getArgument(0);
Message message = new Message();

Bundle data = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private CreativeModelsMaker.Result createModelResult(List<CreativeModel> creativ
CreativeModelsMaker.Result result = new CreativeModelsMaker.Result();
result.creativeModels = creativeModels;
result.transactionState = state;
result.loaderIdentifier = "123";
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -48,21 +47,21 @@ public void setUp() throws Exception {
@Test
public void whenMakeModelsAndNoAdConfiguration_CallErrorListener() {
mModelMakerBids.makeModels(null, mock(BidResponse.class));
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), anyString());
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), any());
}

@Test
public void whenMakeModelsAndNoBidResponse_CallErrorListener() {
mModelMakerBids.makeModels(mock(AdConfiguration.class), null);
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), anyString());
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), any());
}

@Test
public void whenMakeModelsAndBidResponseWithError_CallErrorListener() {
BidResponse mockResponse = mock(BidResponse.class);
when(mockResponse.hasParseError()).thenReturn(true);
mModelMakerBids.makeModels(null, mockResponse);
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), anyString());
verify(mMockLoadListener).onFailedToLoadAd(any(AdException.class), any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -71,7 +70,7 @@ public void testMakeModelsInline() throws Exception {

// Null ad configuration
creativeModelsMakerVast.makeModels(null, rootParser, latestParser);
verify(mMockListener).onFailedToLoadAd(any(AdException.class), anyString());
verify(mMockListener).onFailedToLoadAd(any(AdException.class), any());

// Valid - Inline
creativeModelsMakerVast.makeModels(mAdConfiguration, rootParser, latestParser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private void mockView() {
when(mMockView.getParent()).thenReturn(mockParent);
when(mMockView.getContext()).thenReturn(mActivity);
doAnswer(invocation -> {
final Rect clipRect = invocation.getArgumentAt(0, Rect.class);
final Rect clipRect = invocation.getArgument(0);
clipRect.right = 200;
clipRect.bottom = 300;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.prebid.mobile.rendering.errors.AdException;
import org.prebid.mobile.rendering.listeners.CreativeResolutionListener;
import org.prebid.mobile.rendering.listeners.CreativeViewListener;
import org.prebid.mobile.rendering.listeners.VideoCreativeViewListener;
import org.prebid.mobile.rendering.models.internal.InternalFriendlyObstruction;
import org.prebid.mobile.rendering.models.internal.MraidEvent;
import org.prebid.mobile.rendering.models.internal.VisibilityTrackerResult;
Expand Down Expand Up @@ -115,7 +114,7 @@ public void loadTest() throws Exception {

ViewPool mockViewPool = mock(ViewPool.class);
when(mockViewPool.getUnoccupiedView(any(Context.class),
any(VideoCreativeViewListener.class),
any(),
any(AdConfiguration.AdUnitIdentifierType.class),
any(InterstitialManager.class)))
.thenReturn(mockPrebidWebViewBanner);
Expand Down Expand Up @@ -155,7 +154,7 @@ public void loadTest() throws Exception {

mHtmlCreative = new HTMLCreative(mContext, mMockModel, mMockOmAdSessionManager, mMockInterstitialManager);
mHtmlCreative.load();
verify(mockPrebidWebViewBanner).loadHTML(anyString(), anyInt(), anyInt());
verify(mockPrebidWebViewBanner).loadHTML(any(), anyInt(), anyInt());
assertEquals(mockPrebidWebViewBanner, mHtmlCreative.getCreativeView());
}

Expand Down Expand Up @@ -249,7 +248,7 @@ public void viewabilityTrackListenerExecution_TrackOnWindowFocusChangeAndOnViewE

doAnswer(invocation -> {
CreativeVisibilityTracker.VisibilityTrackerListener listener =
invocation.getArgumentAt(0, CreativeVisibilityTracker.VisibilityTrackerListener.class);
invocation.getArgument(0);

listener.onVisibilityChanged(result);
return null;
Expand Down Expand Up @@ -337,7 +336,7 @@ public void handleMRAIDEventsInCreativeTest() throws Exception {
verify(mMockMraidController).handleMraidEvent(any(MraidEvent.class),
eq(mHtmlCreative),
any(WebViewBase.class),
any(PrebidWebViewBase.class));
any());
}

@Test
Expand All @@ -358,7 +357,7 @@ public void createOmAdSessionTest() throws IllegalAccessException {
mHtmlCreative.setCreativeView(mockOXWebView);

mHtmlCreative.createOmAdSession();
verify(mMockOmAdSessionManager).initWebAdSessionManager(any(WebViewBase.class), anyString());
verify(mMockOmAdSessionManager).initWebAdSessionManager(any(WebViewBase.class), any());
verify(mMockOmAdSessionManager).registerAdView(any(View.class));
verify(mMockOmAdSessionManager).startAdSession();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void closeThroughJSTest() throws Exception {
mraidVariableContainer.setCurrentState(JSInterface.STATE_EXPANDED);
mMraidClose.closeThroughJS();
verify(mSpyBaseJSInterface).onStateChange(eq(JSInterface.STATE_DEFAULT));
verify(mockViewGroup).removeView(any(View.class));
verify(mockViewGroup).removeView(any());

reset(mSpyBaseJSInterface);
AdBrowserActivity mockActivity = new AdBrowserActivity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setup() {
public void expandTest() {

doAnswer(invocation -> {
RedirectUrlListener listener = invocation.getArgumentAt(1, RedirectUrlListener.class);
RedirectUrlListener listener = invocation.getArgument(1);
listener.onSuccess("test", "html");
return null;
}).when(mSpyBaseJsInterface).followToOriginalUrl(anyString(), any(RedirectUrlListener.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public void setUp() throws Exception {
when(mSpyBaseJsInterface.getMraidVariableContainer()).thenReturn(mMockMraidVariableContainer);

when(mMockWebViewBase.post(any(Runnable.class))).thenAnswer(invocation -> {
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
});
doAnswer(invocation -> {
Handler handler = invocation.getArgumentAt(0, Handler.class);
Handler handler = invocation.getArgument(0);
Message message = new Message();
Bundle data = new Bundle();
data.putString(JSInterface.JSON_VALUE, "{\"width\":320,\"height\":250,\"customClosePosition\":\"top-right\",\"offsetX\":0,\"offsetY\":0,\"allowOffscreen\":true}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setup() {
when(mMockWebViewBase.post(any(Runnable.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void setup() {
public void openTest() {

doAnswer(invocation -> {
RedirectUrlListener listener = invocation.getArgumentAt(1, RedirectUrlListener.class);
listener.onSuccess(invocation.getArgumentAt(0, String.class), "html");
RedirectUrlListener listener = invocation.getArgument(1);
listener.onSuccess(invocation.getArgument(0), "html");

return null;
}).when(mMockBaseJsInterface).followToOriginalUrl(anyString(), any(RedirectUrlListener.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -48,15 +48,15 @@ public void whenStartAdRequestAndContextNull_OnErrorWithExceptionCalled() {
mAdConfiguration.setConfigId("test");
BidRequester requester = new BidRequester(null, mAdConfiguration, mAdRequestInput, mMockResponseHandler);
requester.startAdRequest();
verify(mMockResponseHandler).onErrorWithException(any(AdException.class), anyInt());
verify(mMockResponseHandler).onErrorWithException(any(AdException.class), anyLong());
}

@Test
public void whenStartAdRequestAndNoConfigId_OnErrorCalled() {
mAdConfiguration.setConfigId(null);
BidRequester requester = new BidRequester(mContext, mAdConfiguration, mAdRequestInput, mMockResponseHandler);
requester.startAdRequest();
verify(mMockResponseHandler).onError(anyString(), anyInt());
verify(mMockResponseHandler).onError(anyString(), anyLong());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.prebid.mobile.rendering.models.AdConfiguration;
import org.prebid.mobile.rendering.models.internal.InternalPlayerState;
import org.prebid.mobile.rendering.session.manager.OmAdSessionManager;
import org.prebid.mobile.rendering.video.vast.AdVerifications;
import org.prebid.mobile.rendering.views.interstitial.InterstitialManager;
import org.prebid.mobile.test.utils.WhiteBox;
import org.robolectric.Robolectric;
Expand All @@ -24,8 +23,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -74,7 +72,7 @@ public void displayTest() throws Exception {

spyVideoCreative.display();

verify(mVideoCreative.mVideoCreativeView).start(anyInt());
verify(mVideoCreative.mVideoCreativeView).start(anyFloat());
}

@Test
Expand All @@ -84,7 +82,7 @@ public void initNativeAdSessionManagerSuccessTest() throws Exception {

spyVideoCreative.createOmAdSession();

verify(mMockOmAdSessionManager).initVideoAdSession(any(AdVerifications.class), anyString());
verify(mMockOmAdSessionManager).initVideoAdSession(any(), any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -39,7 +40,7 @@ public void startTest() throws IllegalAccessException {
WhiteBox.field(VideoCreativeView.class, "mExoPlayerView").set(mVideoCreativeView, mockPlugPlayView);

mVideoCreativeView.start(anyInt());
verify(mockPlugPlayView).start(anyInt());
verify(mockPlugPlayView).start(anyFloat());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void hideTest() throws IllegalAccessException {
verify(mockAdView, never()).removeView(any(View.class));
WhiteBox.field(AdViewManager.class, "mCurrentCreative").set(mAdViewManager, mockCreative);
mAdViewManager.hide();
verify(mockAdView).removeView(any(View.class));
verify(mockAdView).removeView(any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void interstitialAdClosed_NotifyVideoDelegate() {

@Test
public void interstitialDialogShown_NotifyInterstitialDelegate() {
mSpyInterstitialManager.interstitialDialogShown(any(ViewGroup.class));
mSpyInterstitialManager.interstitialDialogShown(mock(ViewGroup.class));

verify(mMockInterstitialManagerDisplayDelegate).interstitialDialogShown(any(ViewGroup.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void setup() throws Exception {
mSpyInterstitialVideo = Mockito.spy(new InterstitialVideo(null, mMockAdView, mMockInterstitialManager, mMockAdConfiguration));

doAnswer(invocation -> {
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(mMockHandler).post(any(Runnable.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void loadMraidExpandPropertiesWebViewNotNull_ExecuteGetExpandProperties()
throws IllegalAccessException {
final String jsonAnswer = "{\"width\":100,\"height\":200,\"useCustomClose\":false,\"isModal\":true}";
doAnswer(invocation -> {
Handler handler = invocation.getArgumentAt(0, Handler.class);
Handler handler = invocation.getArgument(0);
Message message = new Message();

Bundle data = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public void destroyTest() throws IllegalAccessException {
when(mockHandler.postDelayed(any(Runnable.class), anyLong())).thenAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
invocation.getArgumentAt(0, Runnable.class).run();
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}
});
Expand Down
Loading

0 comments on commit 123918b

Please sign in to comment.