Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualMachine error when trying to mock AdView class

In my Android app I want to test some methods of an activity that configure and then load my Ads.

I'm using ActivityInstrumentationTestCase2 on my MainActivity

To avoid loading the real Ads during tests I would like to mock these two classes and then verify them.

import com.google.ads.AdRequest;
import com.google.ads.AdView;

I'm using mockito, dexmaker and dexmaker-mockito. All that works fine, and I also track code coverage using Emma.

However it seems that on some versions of Android these classes cannot be mocked, as AdView extends RelativeLayout and it has some final methods that DexMaker can't handle.

This results in this trace during testing:

     [exec] Error in testAdWithLocation:
     [exec] java.lang.VirtualMachineError
     [exec]     at dalvik.system.DexFile.defineClass(Native Method)
     [exec]     at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:207)
     [exec]     at dalvik.system.DexFile.loadClass(DexFile.java:196)
     [exec]     at dalvik.system.DexClassLoader.findClass(DexClassLoader.java:226)
     [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
     [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
     [exec]     at com.google.dexmaker.stock.ProxyBuilder.loadClass(ProxyBuilder.java:272)
     [exec]     at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:254)
     [exec]     at com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:56)
     [exec]     at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
     [exec]     at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
     [exec]     at org.mockito.Mockito.mock(Mockito.java:1243)
     [exec]     at org.mockito.Mockito.mock(Mockito.java:1120)
     [exec]     at net.mackenzie_serres.meteowidget.ui.activities.MainActivityTests.testAdWithLocation(MainActivityTests.java:430)
     [exec]     at java.lang.reflect.Method.invokeNative(Native Method)
     [exec]     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
     [exec]     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
     [exec]     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
     [exec]     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
     [exec]     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
     [exec]     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
     [exec]     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

To avoid this I was able to mock AdRequest and work with the real AdView, but I suspect it was the cause I have been blacklisted by Google and my Ad account frozen.

Any ideas on how to be able to test this back to API 9, but not get blacklisted?

thanks

like image 830
Andrew Mackenzie Avatar asked Jan 28 '26 19:01

Andrew Mackenzie


1 Answers

Create a thin abstraction layer over the Admob library and mock that abstraction instead.

This probably means you won't be able to directly put AdView in your Fragment/Activity layout.

like image 144
MaciejGórski Avatar answered Jan 31 '26 10:01

MaciejGórski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!