Robolectric 获取Context问题.

大尾巴狼
  • 245

我使用了Robolectric在做单元测测试,但是怎么获取Context呢?
这是我的代码

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class NiaoSuanTest {

    @Test
    public void testNiaoSuan() {

        MonitorEngine engine = new MonitorEngine(context);
        String residentId = (String) SPUtil.get(context, ResidentInfo.ID, "");
        String gender = (String) SPUtil.get(context, ResidentInfo.GENDER, "1");
        Date date = TimeUtil.paresDateByStr((String) SPUtil.get(context, ResidentInfo.BIRTHDAY, ""));


        Map<Integer, String> param = new HashMap<>();
        param.put(1, "5.5");

        NewMonitorResult monitorResult = engine.monitorProcessByManualdrive("683edfb67a6311e69f52d017c2939671", param, residentId, Integer.parseInt(gender), date, null);
        if (monitorResult != null) {
            Map<String, Object> params = new HashMap<>();
            params.put("data", monitorResult);
            Zcareze.javascript("DeviceMonitor", params);
        } else {
            Zcareze.javascript("DeviceMonitor", null);
        }
    }
}

我使用了: Context applicationContext = getApplicationContext()
还有 Context context = new MockContext();
都不管用. 求大神帮忙! 谢谢.

回复
阅读 2.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏