Can't unit test with Esri's Android Runtime SDK

1619
6
03-06-2020 06:14 AM
PlinioSantos
New Contributor III

I'm writing some unit tests in my android application and every call to Esri's Android Runtime SDK throws a null pointer exeption.

The method

@Test
public void addition_isCorrect() {
    SpatialReference wgs84 = SpatialReferences.getWgs84();
    assertEquals(4, 2 + 2);
}

... breaks with the following stack trace:

java.lang.ExceptionInInitializerError
at com.esri.arcgisruntime.internal.jni.CoreSpatialReference.<clinit>(SourceFile:40)
at com.esri.arcgisruntime.geometry.SpatialReference.<init>(SourceFile:149)
at com.esri.arcgisruntime.geometry.SpatialReference.create(SourceFile:86)
at com.esri.arcgisruntime.geometry.SpatialReferences.<clinit>(SourceFile:26)
at br.gov.ba.conder.vistorias.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
at com.esri.arcgisruntime.ArcGISRuntimeEnvironment.c(SourceFile:250)
at com.esri.arcgisruntime.ArcGISRuntimeEnvironment.<clinit>(SourceFile:50)
... 27 more

What I'm doing wrong?

I'm using the Android Runtime SDK 100.6.0

0 Kudos
6 Replies
GayleYoung
Esri Contributor

Hi,

what Android OS version are you using (from android.os.Build.VERSION.RELEASE)? Also, does the Android SDK work for you in a very basic mapping application; such as a Sample? It sounds like the ArcGISRuntimeEnvironment is not initialized yet. There may be a good reason for that or it may be that we should force initialization. I need a little more information to work out whether we should expect this environment to initialize.

0 Kudos
PlinioSantos
New Contributor III

Gayle, since the error is regarding unit testing (the test source set), the android version should not matter because those tests runs outside a device or emulator.

Anyway, the android version I'm currently using is API 28 (android.os.Build.VERSION.RELEASE = 9). The application is working fine and I can navigate maps, add graphics and so on, just the unit tests are getting that error.

In order to use ArcGIS Runtime SDK for Android in tests both unit and instrumentation, have I to do any inicialization?

0 Kudos
PlinioSantos
New Contributor III

More info: I can run instrumentated tests containing calls to ArcGIS Runtime. The mais difference between unit tests and instrumentated test are the the last ones must run in a device or emulator. So, it really seems that unit tests are missing some library initialization. How can I force the inicialization of ArcGIS Runtime for android?

0 Kudos
GayleYoung
Esri Contributor

Hello, thanks for the information. You can try to call ArcGISRuntime.initialize() to force the initialization of ArcGIS Runtime. However, I'm not clear if your unit tests are running in a headless environment and I believe the product was designed to work on a device; so you will likely still need a graphical environment to run the unit tests.

0 Kudos
PlinioSantos
New Contributor III

Unit tests still dont work with ArcGISRuntime.initialize(). I guess you're right and ArcGIS Runtime for Android must run in actual devices or emulators. This is a shame once unit testing is a good development practice.

0 Kudos
padmalcom
Occasional Contributor

Hi, two years later, I'm running into the same issue with Kotlin. Can't believe nobody does unit testing in the meantime. Could you solve the issue @PlinioSantos?