UnsatisfiedLinkError while unit testing WritableNativeMap

I am currently creating an Android library for use in a reciprocal native project. I need to throw the card in javascript, so I use my own WriteableMap class. Unfortunately, the class loads reactive SO in a static block, which causes an UnsatisfiedLinkError during unit tests. I use JUnit and Mockito for testing.

My code is:

@Override public void onSomething() { WritableMap params = Arguments.createMap(); //fill map sendEvent("onChange", params); } 

Error:

 java.lang.UnsatisfiedLinkError: no reactnativejni in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1119) at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:172) at com.facebook.react.bridge.NativeMap.<clinit>(NativeMap.java:23) at com.facebook.react.bridge.Arguments.createMap(Arguments.java:29) at me.MyClass.onSomething(myClass.java:23) 

I started using the Arguments.createMap() method, seeing a comment about doing Stubbing WriteableMap for unit tests, but it is static, and I would prefer not to stop the static method.

Is there a way to get rid of this error when doing unit tests?

+7
source share
2 answers

I do not think that you can avoid ridicule of the Arguments methods in the unit test (although I suppose that you do not need to do this in the instrumental test).

In their own Facebook tests, they use PowerMockito to mock them: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/test/java/com/facebook/react/RootViewTest.java#L67

Interesting bits:

 PowerMockito.mockStatic(Arguments.class); PowerMockito.when(Arguments.createArray()).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return new JavaOnlyArray(); } }); PowerMockito.when(Arguments.createMap()).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return new JavaOnlyMap(); } }); 

Also note that you need to change your build.gradle include the following tools for ridicule: https://github.com/facebook/react-native/blob/master/ReactAndroid/build.gradle#L266

 dependencies { ... testCompile "org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}" testCompile "org.powermock:powermock-module-junit4-rule:${POWERMOCK_VERSION}" testCompile "org.powermock:powermock-classloading-xstream:${POWERMOCK_VERSION}" testCompile "org.mockito:mockito-core:${MOCKITO_CORE_VERSION}" testCompile "org.easytesting:fest-assert-core:${FEST_ASSERT_CORE_VERSION}" testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}" ... } 

The versions they use can be found in their gradle.properties file.

I donโ€™t know how stable these test configurations will be in the long run, but this configuration allowed me to work with ReadableMap / Array and WritableMap / Array in unit tests.

+6
source

You can also use JavaOnlyMap , it implements both WritableMap and ReadableMap .

So wherever you need to use Arguments.createMap() , just replace it with new JavaOnlyMap()

link: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaOnlyMap.java

0
source

All Articles