首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元测试Android服务

单元测试Android服务
EN

Stack Overflow用户
提问于 2015-11-25 19:21:05
回答 1查看 564关注 0票数 0

我试图使用"ServiceTestCase“框架测试我的服务,如下所示:

代码语言:javascript
复制
@RunWith(AndroidJUnit4.class)
public class testTts extends ServiceTestCase<TtsLbEngine> {

    public testTts() {
        super(TtsLbEngine.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }


    @Test
    public void testOnCreate() throws Exception {
        Intent intent = new Intent(getSystemContext(), TtsLbEngine.class);
        intent.setClass(getSystemContext(), TtsLbEngine.class);
        startService(intent);
        assertNotNull(getService());

    }
}

问题是服务是Null。你能告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-26 10:55:21

我设法解决了这个问题。请在下面找到如何做到这一点:

代码语言:javascript
复制
public class testService extends ServiceTestCase<myService> {


    public testService() {
        super(myService.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }


    @Test
    public void testService() throws Exception {
        Intent startIntent = new Intent();

        startIntent.setClass(getContext(), myService.class);
        startService(startIntent);

        myService s = getService();

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

https://stackoverflow.com/questions/33924799

复制
相关文章

相似问题

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