Skip to content

LocationPickerActivity is showing MapboxConfigurationException while creating it in a test class #4820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ayan-10 opened this issue Feb 15, 2022 · 11 comments

Comments

@Ayan-10
Copy link
Contributor

Ayan-10 commented Feb 15, 2022

Description

I was trying to add tests for the LocationPickerActivity class. But getting errors while building the class. Can't find any solution for that.

What I was doing

2022-02-14

Error

Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class com.mapbox.mapboxsdk.maps.MapView
	at android.view.LayoutInflater.createView(LayoutInflater.java:633)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
	at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
	at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
	at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
	at fr.free.nrw.commons.LocationPicker.LocationPickerActivity.onCreate(LocationPickerActivity.java:139)
	at android.app.Activity.performCreate(Activity.java:5933)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
	at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:730)
	at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:106)
	at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
	at fr.free.nrw.commons.locationpicker.LocationPickerActivityUnitTests.setUp(LocationPickerActivityUnitTests.kt:32)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:568)

Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createView(LayoutInflater.java:607)
	at android.view.LayoutInflater.createView(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:743)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:806)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$parseInclude(LayoutInflater.java:916)
	at android.view.LayoutInflater.parseInclude(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:802)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:504)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:414)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:365)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
	at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
	at fr.free.nrw.commons.LocationPicker.LocationPickerActivity.onCreate(LocationPickerActivity.java:139)
	at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:5933)
	at android.app.Activity.performCreate(Activity.java)
	at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1105)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
	at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:730)
	at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:106)
	at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
	at fr.free.nrw.commons.locationpicker.LocationPickerActivityUnitTests.setUp(LocationPickerActivityUnitTests.kt:32)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)

Caused by: com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException: 
Using MapView requires calling Mapbox.getInstance(Context context, String accessToken) before inflating or creating the view. The access token parameter is required when using a Mapbox service.
Please see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one.
More information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens.
	at com.mapbox.mapboxsdk.maps.MapView.initialize(MapView.java:132)
	at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:108)

I think the exception is occurring because of using Mapview and it's asking for this Mapbox.getInstance(Context context, String accessToken). But NearbyParentFragment also is using mapview in the same way, it's not giving error while inflating the layout in the test.

@madhurgupta10
Copy link
Collaborator

Looking at the logs, I guess the problem here is that Mapbox needs an actual device to get initialised properly. There could be two possible ways to write tests for this class:

  1. Find a way to get a test instance of Mapbox (ideal solution)
  2. Use Mock reflection of Mapbox similar to NearbyParentFragment Tests

@Ayan-10
Copy link
Contributor Author

Ayan-10 commented Feb 15, 2022

2. Use Mock reflection of Mapbox similar to NearbyParentFragment Tests

@madhurgupta10 I tried this earlier but the same exception occurred.

@madhurgupta10
Copy link
Collaborator

  1. Use Mock reflection of Mapbox similar to NearbyParentFragment Tests

@madhurgupta10 I tried this earlier but the same exception occurred.

You need to avoid .create() method for activity, it directly calls the onCreate

@Ayan-10
Copy link
Contributor Author

Ayan-10 commented Feb 16, 2022

@madhurgupta10 I think without calling .create() onCreate will not be covered. Isn't it?

@arinmodi
Copy link
Contributor

@Ayan-10 , Now I understand the testing and start working on LocationPickerActivityTest, but same error is occured with me,
find any solution ?

@arinmodi
Copy link
Contributor

@madhurgupta10, same error happened with me, any solution ?

@madhurgupta10
Copy link
Collaborator

@madhurgupta10 I think without calling .create() onCreate will not be covered. Isn't it?

Yes, for now, you can skip onCreate and write tests for other methods

@madhurgupta10
Copy link
Collaborator

@madhurgupta10, same error happened with me, any solution ?

@arinmodi Refer #4820 (comment)

@arinmodi
Copy link
Contributor

@madhurgupta10, tried but not worked...

@madhurgupta10
Copy link
Collaborator

@Ayan-10 After (#4828) is merged you can try writing instrumentation tests for LocationPickerActivity instead of robolectric

@Ayan-10
Copy link
Contributor Author

Ayan-10 commented Feb 17, 2022

@madhurgupta10 That's great. Sure I will add instrumentation tests too. However, I created unit tests for the class covering as much as possible.

Thanks a lot for the help.

@Ayan-10 Ayan-10 closed this as completed Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants