ServiceTestCase<T>.getService?

烂漫一生 提交于 2019-12-03 03:46:27

This is what you need to test your service

public class MyServiceTests extends ServiceTestCase<MyService> {

private static final String TAG = "MyServiceTests";

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

/**
 * Test basic startup/shutdown of Service
 */
@SmallTest
public void testStartable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    startService(startIntent);
    assertNotNull(getService());
}

/**
 * Test binding to service
 */
@MediumTest
public void testBindable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    IBinder service = bindService(startIntent);
    assertNotNull(service);
}
}

I've written some articles about Android testing and test driven development that you may find useful, check http://dtmilano.blogspot.com/search/label/test%20driven%20development.

The accepted answer doesn't work any more.

TestCases like ActivityInstrumentationTestCase2 or ServiceTestCase are deprecated in favor of ActivityTestRule or ServiceTestRule.

ATSL link

It seems they forgot to update the actual documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!