mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

Does Mapbox Support Robolectric testing?

Open erluxman opened this issue 7 years ago • 7 comments

Steps to reproduce

  1. I was trying to initialize Mapbox by using Mapbox.getInstance(this, BuildConfig.MAPBOX_ACCESS_KEY)
  2. But when I try to run the Robolectric test : I get

java.lang.UnsatisfiedLinkError: com.mapbox.mapboxsdk.net.NativeConnectivityListener.initialize()V

Expected behavior: The test should pass.

Actual behavior: Full Stacktress


java.lang.UnsatisfiedLinkError: com.mapbox.mapboxsdk.net.NativeConnectivityListener.initialize()V

	at com.mapbox.mapboxsdk.net.NativeConnectivityListener.initialize(Native Method)
	at com.mapbox.mapboxsdk.net.NativeConnectivityListener.<init>(NativeConnectivityListener.java:27)
	at com.mapbox.mapboxsdk.net.ConnectivityReceiver.instance(ConnectivityReceiver.java:43)
	at com.mapbox.mapboxsdk.Mapbox.getInstance(Mapbox.java:60)
	at com.example.testapp.App.onCreate(App.kt:11)
	at org.robolectric.android.internal.AndroidEnvironment.lambda$installAndCreateApplication$0(AndroidEnvironment.java:253)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:75)
	at org.robolectric.android.internal.AndroidEnvironment.installAndCreateApplication(AndroidEnvironment.java:253)
	at org.robolectric.android.internal.AndroidEnvironment.setUpApplicationState(AndroidEnvironment.java:149)
	at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:298)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:247)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Configuration

Android versions: Any Device models: Any Mapbox SDK versions:Any

erluxman avatar Apr 24 '19 06:04 erluxman

@erluxman yes we do support them, we also use them ourselves for testing the SDK for some scenarios. The crash you are hitting can be expected as you are trying to load/access a native library on the JVM of your development machine(vs instrumented on a device) while the shared objects are created specifically for android. This won't work and that it doesn't work is a works as designed.

Solutions to this either range from mocking components that require native code or running an instrumented unit test instead so you are able to load to shared object on the JVM of an Android device.

tobrun avatar Apr 24 '19 06:04 tobrun

Are there any sample of testing Mapbox for Android App which uses Mapbox and has other UIs rather than testing just MapBox?

I can't run any kind of Robolectric test due to above error.

erluxman avatar Jun 05 '19 09:06 erluxman

As indicated above, you need to mock components that load native libraries. Examples from our SDK can be found in https://github.com/mapbox/mapbox-gl-native/tree/master/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk

tobrun avatar Jun 05 '19 10:06 tobrun

@erluxman If this is happening due to the initialization in your Application class, you can use a TestApplication class and exclude the initialization from that file. For tests where the actual MapView is used, you can write a shadow for that class.

saadfarooq avatar Jun 07 '19 21:06 saadfarooq

@saadfarooq do you have any example of using TestApplication? Sorry that I am very very very beginner with Testing, let alone the Robolectric.

erluxman avatar Jun 08 '19 07:06 erluxman

This issue is duplicate and I solved it in #14428

caovanthanh203 avatar Feb 13 '20 17:02 caovanthanh203

Is this issue actually a duplicated from #14428 ? the proposed answer to that thread wont work. I believe the real question is:

  • which is the recomended way to made unit test using robolectric on an android project with mapbox? For the time beign I'll try mocking the lib or using a testApplication as pointed, but I'm not sure if this is the recomended way

EricVanDerDijs avatar Oct 06 '20 11:10 EricVanDerDijs