使用网上所说的AndroidTestCase的getContext方法,获取到的context为空。为什么这个方法获取的context为空,或者有什么其他的在单元测试中获取context的方法?
这是代码:
public class UtilsTests extends AndroidTestCase{
private String dateTime = "2016-6-12 5:33:32";
private Context context;
@Before
public void setUp() throws Exception {
super.setUp();
context = getContext();
}
@Test
public void testGetPath() throws Exception {
assertEquals(getContext(),null);
}
}
断言判断出来context为null
在stackoverflow找到了解决的办法,Context context = new MockContext();
附上链接:http://stackoverflow.com/questions/28960898/getting-context-in-androidtestcase-or-instrumentationtestcase-in-android-studio