You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为带有安卓原生模块的React Native应用编写单元测试?

为React Native安卓原生模块编写单元测试的技术方案

嘿,针对你正在构建的带安卓原生模块的React Native应用,我整理了一套编写单元测试的实用方案,结合你给出的代码结构来拆解:

1. 先搞定测试环境配置

首先确保你的安卓项目已经配置好基础测试依赖,React Native项目一般默认会带JUnit,但可以检查下app/build.gradle里的dependencies块,补充必要的依赖:

dependencies {
    // 其他现有依赖...
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:4.5.1' // 用来模拟React上下文这类依赖对象
}

2. 测试自定义Native Module

先补全你没写完的MyCustomPackage代码(假设你的原生模块是MyCustomModule):

public class MyCustomPackage implements ReactPackage {
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new MyCustomModule(reactContext));
        return modules;
    }
}

接下来在app/src/test/java/com/your-app-package/目录下创建测试类MyCustomModuleTest.java,用Mockito模拟React上下文,测试模块的方法:

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.facebook.react.bridge.ReactApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

public class MyCustomModuleTest {
    @Mock
    private ReactApplicationContext mockContext;

    private MyCustomModule customModule;

    @Before
    public void setUp() {
        MockitoAnnotations.openMocks(this); // 初始化Mock对象
        // 如果你的模块需要上下文的特定返回值,在这里Mock
        // 比如 when(mockContext.getPackageName()).thenReturn("com.yourapp");
        customModule = new MyCustomModule(mockContext);
    }

    // 测试同步方法示例
    @Test
    public void testSyncCustomMethod() {
        // 假设你的模块有个getGreeting方法,接收名字返回问候语
        String greeting = customModule.getGreeting("Harshitha");
        assertEquals("Hello Harshitha!", greeting); // 验证结果是否符合预期
    }

    // 测试异步方法(比如带Promise的方法)示例
    @Test
    public void testAsyncCustomMethod() {
        customModule.fetchSomeData(new Promise() {
            @Override
            public void resolve(Object value) {
                assertEquals("Expected Data", value);
            }

            @Override
            public void reject(String code, String message) {
                // 如果这个方法不该失败,这里直接断言失败
                assert false : "该方法未按预期执行,调用了reject";
            }

            // 实现Promise接口的其他方法...
        });
    }
}

3. 测试ReactPackage的注册逻辑

这部分测试比较简单,但能确保你的Package正确返回模块:

import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.facebook.react.bridge.ReactApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MyCustomPackageTest {
    @Mock
    private ReactApplicationContext mockContext;

    public MyCustomPackageTest() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void testCreateNativeModules() {
        MyCustomPackage customPackage = new MyCustomPackage();
        assertEquals(1, customPackage.createNativeModules(mockContext).size());
        // 验证返回的模块是目标类型
        assertTrue(customPackage.createNativeModules(mockContext).get(0) instanceof MyCustomModule);
    }

    @Test
    public void testCreateViewManagers() {
        MyCustomPackage customPackage = new MyCustomPackage();
        assertEquals(0, customPackage.createViewManagers(mockContext).size());
    }
}

4. 可选:集成测试(RN与原生交互)

如果需要验证JS层调用原生方法的完整流程,可以使用React Native的测试工具做instrumented测试,不过这部分相对复杂,若只是验证原生方法的逻辑,前面的单元测试就足够覆盖了。

内容的提问来源于stack exchange,提问作者Harshitha Palihawadana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:49:55