11package com .d4rk .androidtutorials .java .ads ;
22
3- import static org .mockito .Mockito .*;
3+ import static org .junit .Assert .assertNotNull ;
4+ import static org .junit .Assert .assertTrue ;
5+ import static org .mockito .Mockito .mockStatic ;
6+ import static org .mockito .Mockito .times ;
47
58import android .content .Context ;
9+ import android .test .mock .MockContext ;
610import android .view .View ;
711
812import com .d4rk .androidtutorials .java .ads .views .NativeAdBannerView ;
2125 */
2226public class AdUtilsTest {
2327
28+ private final TestContext context = new TestContext ();
29+
2430 @ Before
2531 public void setUp () throws Exception {
2632 // Reset the initialized flag before each test
@@ -31,9 +37,6 @@ public void setUp() throws Exception {
3137
3238 @ Test
3339 public void initialize_callsMobileAdsInitializeOnlyOnce () {
34- Context context = mock (Context .class );
35- when (context .getApplicationContext ()).thenReturn (context );
36-
3740 try (MockedStatic <MobileAds > mobileAds = mockStatic (MobileAds .class )) {
3841 AdUtils .initialize (context );
3942 AdUtils .initialize (context );
@@ -44,44 +47,84 @@ public void initialize_callsMobileAdsInitializeOnlyOnce() {
4447
4548 @ Test
4649 public void loadBanner_withAdView_loadsAd () {
47- Context context = mock (Context .class );
48- when (context .getApplicationContext ()).thenReturn (context );
49- AdView adView = mock (AdView .class );
50- when (adView .getContext ()).thenReturn (context );
50+ FakeAdView adView = new FakeAdView (context );
5151
5252 try (MockedStatic <MobileAds > mobileAds = mockStatic (MobileAds .class )) {
5353 AdUtils .loadBanner (adView );
5454 mobileAds .verify (() -> MobileAds .initialize (context ));
5555 }
5656
57- verify (adView , times (1 )).loadAd (any (AdRequest .class ));
57+ assertTrue (adView .isLoadAdCalled ());
58+ assertNotNull (adView .getLastRequest ());
5859 }
5960
6061 @ Test
6162 public void loadBanner_withNativeAdBannerView_loadsAd () {
62- Context context = mock (Context .class );
63- when (context .getApplicationContext ()).thenReturn (context );
64- NativeAdBannerView nativeView = mock (NativeAdBannerView .class );
65- when (nativeView .getContext ()).thenReturn (context );
63+ FakeNativeAdBannerView nativeView = new FakeNativeAdBannerView (context );
6664
6765 try (MockedStatic <MobileAds > mobileAds = mockStatic (MobileAds .class )) {
6866 AdUtils .loadBanner (nativeView );
6967 mobileAds .verify (() -> MobileAds .initialize (context ));
7068 }
7169
72- verify (nativeView , times ( 1 )). loadAd ( );
70+ assertTrue (nativeView . isLoadAdCalled () );
7371 }
7472
7573 @ Test
7674 public void loadBanner_withOtherView_doesNothing () {
77- Context context = mock (Context .class );
78- when (context .getApplicationContext ()).thenReturn (context );
79- View view = mock (View .class );
80- when (view .getContext ()).thenReturn (context );
75+ View view = new View (context );
8176
8277 try (MockedStatic <MobileAds > mobileAds = mockStatic (MobileAds .class )) {
8378 AdUtils .loadBanner (view );
8479 mobileAds .verifyNoInteractions ();
8580 }
81+
82+ }
83+
84+ private static final class TestContext extends MockContext {
85+ @ Override
86+ public Context getApplicationContext () {
87+ return this ;
88+ }
89+ }
90+
91+ private static final class FakeAdView extends AdView {
92+ private boolean loadAdCalled ;
93+ private AdRequest lastRequest ;
94+
95+ FakeAdView (Context context ) {
96+ super (context );
97+ }
98+
99+ @ Override
100+ public void loadAd (AdRequest adRequest ) {
101+ loadAdCalled = true ;
102+ lastRequest = adRequest ;
103+ }
104+
105+ boolean isLoadAdCalled () {
106+ return loadAdCalled ;
107+ }
108+
109+ AdRequest getLastRequest () {
110+ return lastRequest ;
111+ }
112+ }
113+
114+ private static final class FakeNativeAdBannerView extends NativeAdBannerView {
115+ private boolean loadAdCalled ;
116+
117+ FakeNativeAdBannerView (Context context ) {
118+ super (context );
119+ }
120+
121+ @ Override
122+ public void loadAd () {
123+ loadAdCalled = true ;
124+ }
125+
126+ boolean isLoadAdCalled () {
127+ return loadAdCalled ;
128+ }
86129 }
87130}
0 commit comments