Android的ServiceTestCase的IntentService [英] Android ServiceTestCase for IntentService
问题描述
我目前正在写单元测试的Android应用程序,并跌进了以下问题:
我用的是 ServiceTestCase
来测试一个 IntentService
是这样的:
@覆盖
公共无效设置()抛出异常{
super.setUp();
}
公共无效TestService的()
{
意向意图=新的意图(getSystemContext(),MyIntentService.class);
super.startService(意向);
assertNotNull(的getService());
}
不过,我发现我的 IntentService
创建(指的onCreate
被称为),但我从来没有接听电话到 onHandleIntent(意向意图)
有没有人已经测试了 IntentService
与 ServiceTestCase
类?
谢谢!
我刚刚开始进入测试自己的 IntentService
和它的证明是一个有点头疼。
还有努力工作的事情了,但对于这样一个场景,你似乎没有收到调用你的方法 onHandleIntent()
,(我不是很好背后 JUnit的技术性
所以请原谅我使用的术语),它应该是因为测试框架,根据您的code,居然眼泪就下来了,或结束测试方法一次您的来电 startService
的回报。没有足够的时间 onHandleIntent
被触发。
onHandleIntent
登录 - 我通过在我的测试情况下,增加一个无限循环验证了上述理论
I'm currently writing unit tests for an android application and stumbled into the following issue:
I use the ServiceTestCase
to test an IntentService
like this:
@Override
public void setUp() throws Exception {
super.setUp();
}
public void testService()
{
Intent intent = new Intent(getSystemContext(), MyIntentService.class);
super.startService(intent);
assertNotNull(getService());
}
However I noticed that my IntentService
is created (means that onCreate
is called) but I never receive a call into onHandleIntent(Intent intent)
Has anyone already tested an IntentService
with the ServiceTestCase
class?
Thanks!
I just got started into testing my own IntentService
and it's proving to be a bit of a headache.
Still trying to work things out but for the scenario where it seems that you do not receive a call to your method onHandleIntent()
, (I'm not very good with the technicalities behind junit
so forgive my use of terminology) it should be because the test framework, based on your code, actually tears down or end the test method once your call to startService
returns. There is insufficient time for onHandleIntent
to be triggered.
I verified the above theory by adding an infinite loop within my test case - only then can I see my log statements in onHandleIntent
logged.
这篇关于Android的ServiceTestCase的IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!