diff --git a/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK b/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK index 9e0251c78..83b2c81f8 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK +++ b/ReactAndroid/src/test/java/com/facebook/react/bridge/BUCK @@ -38,6 +38,7 @@ robolectric3_test( react_native_dep('third-party/java/jackson:jackson'), react_native_dep('third-party/java/jsr-305:jsr-305'), react_native_dep('libraries/fbcore/src/test/java/com/facebook/powermock:powermock'), + react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'), ], visibility = [ 'PUBLIC' diff --git a/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java new file mode 100644 index 000000000..8a681ccb5 --- /dev/null +++ b/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.bridge; + +import java.util.Map; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.ReadableNativeArray; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.runner.RunWith; +import org.junit.Test; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.robolectric.RobolectricTestRunner; + +import com.facebook.soloader.SoLoader; + +/** + * Tests for {@link BaseJavaModule} + */ +@PrepareForTest({ReadableNativeArray.class, SoLoader.class}) +@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) +@RunWith(RobolectricTestRunner.class) +public class BaseJavaModuleTest { + + @Rule + public PowerMockRule rule = new PowerMockRule(); + + private Map mMethods; + private ReadableNativeArray mArguments; + + @Before + public void setup() { + mMethods = new MethodsModule().getMethods(); + PowerMockito.mockStatic(SoLoader.class); + mArguments = PowerMockito.mock(ReadableNativeArray.class); + } + + @Test(expected = NativeArgumentsParseException.class) + public void testCallMethodWithoutEnoughArgs() throws Exception { + BaseJavaModule.NativeMethod regularMethod = mMethods.get("regularMethod"); + Mockito.stub(mArguments.size()).toReturn(1); + regularMethod.invoke(null, mArguments); + } + + @Test(expected = NativeArgumentsParseException.class) + public void testCallAsyncMethodWithoutEnoughArgs() throws Exception { + BaseJavaModule.NativeMethod asyncMethod = mMethods.get("asyncMethod"); + Mockito.stub(mArguments.size()).toReturn(2); + asyncMethod.invoke(null, mArguments); + } + + @Test() + public void testCallAsyncMethodWithEnoughArgs() throws Exception { + BaseJavaModule.NativeMethod asyncMethod = mMethods.get("asyncMethod"); + Mockito.stub(mArguments.size()).toReturn(3); + asyncMethod.invoke(null, mArguments); + } + + private static class MethodsModule extends BaseJavaModule { + @Override + public String getName() { + return "Methods"; + } + + @ReactMethod + public void regularMethod(String a, int b) { + } + + @ReactMethod + public void asyncMethod(int a, Promise p) { + } + } +}