찾다

 >  Q&A  >  본문

android - Robolectric 获取Context问题.

我使用了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();
都不管用. 求大神帮忙! 谢谢.

PHPzPHPz2771일 전585

모든 응답(0)나는 대답할 것이다

답장 없음
  • 취소회신하다