简体   繁体   中英

Mock/stub Arguments.createMap during testing

In the React Native library theres a class https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/Arguments.java that is used for Bundles. I'm using PowerMockito to mock the Arguments.createMap() method and return an object with the following snippet:

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

The method I'm testing errors with the following message when the test is ran:

java.lang.UnsatisfiedLinkError: no reactnativejni in java.library.path

when executing this line:

WritableMap map = Arguments.createMap();

Any ideas?

Extract the Answer<Object> to a variable. Mockito doesnt like it when you use the new operator as a parameter.

Try something like this:

Answer<Object> answer = new Answer<Object>() {
  @Override
  public Object answer(InvocationOnMock invocation) throws Throwable {
    return new JavaOnlyMap();
  }
}

PowerMockito.when(Arguments.createMap()).thenAnswer(answer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM