MoPubCustomEventNative extends CustomEventNative { @Override protected void loadNativeAd(@NonNull final Context context, @NonNull final CustomEventNativeListener customEventNativeListener, @NonNull final Map<String, Object> localExtras, @NonNull final Map<String, String> serverExtras) { final MoPubForwardingNativeAd moPubForwardingNativeAd = new MoPubForwardingNativeAd(context.getApplicationContext(), serverExtras.get(RESPONSE_BODY_KEY), customEventNativeListener); try { moPubForwardingNativeAd.loadAd(); } catch (IllegalArgumentException e) { customEventNativeListener.onNativeAdFailed(NativeErrorCode.UNSPECIFIED); MoPubEvents.log(new ErrorEvent.Builder("","").withException(e).build()); } catch (JSONException e) { customEventNativeListener.onNativeAdFailed(NativeErrorCode.INVALID_JSON); MoPubEvents.log(new ErrorEvent.Builder("","").withException(e).build()); } }    }
@Test public void loadNativeAd_withInvalidResponseBody_shouldNotifyListenerOfOnNativeAdFailed() throws Exception { serverExtras.put(CustomEventNativeAdapter.RESPONSE_BODY_KEY, "{ \"bad json"); subject.loadNativeAd(context, mCustomEventNativeListener, localExtras, serverExtras); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(MoPubCustomEventNative.MoPubForwardingNativeAd.class)); verify(mCustomEventNativeListener).onNativeAdFailed(NativeErrorCode.INVALID_JSON); }
@Test public void loadNativeAd_withNullResponseBody_shouldNotifyListenerOfOnNativeAdFailed() throws Exception { serverExtras.put(CustomEventNativeAdapter.RESPONSE_BODY_KEY, null); subject.loadNativeAd(context, mCustomEventNativeListener, localExtras, serverExtras); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(MoPubCustomEventNative.MoPubForwardingNativeAd.class)); verify(mCustomEventNativeListener).onNativeAdFailed(NativeErrorCode.UNSPECIFIED); }