2

我对下面的代码进行了测试,但我得到了 NullpointerException,因为 getSystemContext() 返回 null。我的项目如下所示:

~/trunk/src/<packages>/<classes>
~/trunk/test/<packages/<testclasses>
~/trunk/<static android dirs>
~/trunk/AndroidManifest.xml

请告诉我我做错了什么。

    @RunWith(RobolectricTestRunner.class)
    public class MyServiceTest extends ServiceTestCase<MyService> {


    public MyServiceTest() {
       super(MyService.class);
    }

    @Test
    public void testStartable() {
       Context mockContext = getSystemContext();
       Log.d("testStartable", "testContext " + mockContext.getClass().getCanonicalName());
       Intent startIntent = new Intent();
       startIntent.setClass(mockContext, MyService.class);
       mockContext.startService(startIntent);
       assertNotNull(getService());
    }
   }
4

0 回答 0