我正在尝试在android上构建一个有效的junit测试套件。
因为我是Junit的新手,所以我不知道如何使用ServiceTestCase类。
我想不出如何让getService()方法工作。它曾经返回给我null。所以我决定通过startService开始。它不起作用。
你能帮帮我吗?
谢谢
发布于 2010-02-20 07:53:55
这就是测试服务所需的内容
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);
}
}我写了一些关于安卓测试和测试驱动开发的文章,你可能会发现它们很有用,请查看http://dtmilano.blogspot.com/search/label/test%20driven%20development。
发布于 2016-06-04 06:11:35
公认的答案不再起作用了。
像ActivityInstrumentationTestCase2或ServiceTestCase这样的
TestCases被弃用,取而代之的是ActivityTestRule或ServiceTestRule。
他们似乎忘记了更新实际的文档。
https://stackoverflow.com/questions/2300029
复制相似问题