最近闲来无事,领导有让做一些单元测试的工作,本来android studio中已经集成了单元测试的功能,本以为做起来很容易,结果还是悲剧了,在测试case中,getContext为空,

经过查找资料,用MockContext初始化就可以了。

参考资料:http://stackoverflow.com/questions/28960898/getting-context-in-androidtestcase-or-instrumentationtestcase-in-android-studio/

public class MainActivityInstrumentationTest2 extends AndroidTestCase{
    public Context context;
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        context = new MockContext();
        assertNotNull(context);
    }

    public void test() throws Exception{
        assertEquals(1,1);
    }
}

 

相关文章:

  • 2021-07-11
  • 2021-07-25
  • 2021-09-11
  • 2021-05-24
  • 2021-06-23
  • 2021-12-01
  • 2022-12-23
猜你喜欢
  • 2021-05-17
  • 2021-12-10
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案