UnsatisfiedLinkError when unit testing WritableNativeMap
You can also use JavaOnlyMap
, it implements both WritableMap
and ReadableMap
.
So wherever you need to use Arguments.createMap()
, just replace with new JavaOnlyMap()
reference: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaOnlyMap.java
I don't think you can avoid mocking the Arguments methods in a unit test (though I believe you do not need to do so in an instrumentation test).
In Facebook's own 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 this requires you to modify your build.gradle
to include these mocking tools:
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 over the long run, but this configuration has let me work with ReadableMap/Array and WritableMap/Array in unit tests.