首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ServiceTestCase<T>.getService?

ServiceTestCase<T>.getService?
EN

Stack Overflow用户
提问于 2010-02-20 06:20:23
回答 2查看 3K关注 0票数 5

我正在尝试在android上构建一个有效的junit测试套件。

因为我是Junit的新手,所以我不知道如何使用ServiceTestCase类。

我想不出如何让getService()方法工作。它曾经返回给我null。所以我决定通过startService开始。它不起作用。

你能帮帮我吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-20 07:53:55

这就是测试服务所需的内容

代码语言:javascript
复制
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

票数 16
EN

Stack Overflow用户

发布于 2016-06-04 06:11:35

公认的答案不再起作用了。

像ActivityInstrumentationTestCase2或ServiceTestCase这样的

TestCases被弃用,取而代之的是ActivityTestRule或ServiceTestRule。

ATSL link

他们似乎忘记了更新实际的文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2300029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档