Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Does Mapbox Support Robolectric testing? #14492

Closed
erluxman opened this issue Apr 24, 2019 · 7 comments
Closed

Does Mapbox Support Robolectric testing? #14492

erluxman opened this issue Apr 24, 2019 · 7 comments
Labels
Android Mapbox Maps SDK for Android

Comments

@erluxman
Copy link

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

@tobrun
Copy link
Member

tobrun commented Apr 24, 2019

@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 tobrun closed this as completed Apr 24, 2019
@tobrun tobrun added the Android Mapbox Maps SDK for Android label Apr 24, 2019
@erluxman
Copy link
Author

erluxman commented Jun 5, 2019

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.

@tobrun
Copy link
Member

tobrun commented Jun 5, 2019

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

@saadfarooq
Copy link

@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.

@erluxman
Copy link
Author

erluxman commented Jun 8, 2019

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

@caovanthanh203
Copy link

This issue is duplicate and I solved it in #14428

@EricVanDerDijs
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

5 participants