Android ServiceTestCase for IntentService
我目前正在为 Android 应用程序编写单元测试,并偶然发现了以下问题:
我使用 ServiceTestCase
来测试 IntentService
,如下所示:
@Override
public void setUp() throws Exception {
super.setUp();
}
public void testService()
{
Intent intent = new Intent(getSystemContext(), MyIntentService.class);
super.startService(intent);
assertNotNull(getService());
}
但是我注意到我的 IntentService
已创建(意味着 onCreate
被调用),但我从未收到对 onHandleIntent(Intent Intent)
的调用
有没有人已经测试过IntentService
与 ServiceTestCase
类?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这有点晚了,但我只是为此苦苦挣扎。您可以通过创建一个类来简单地覆盖服务的 onStart 来解决此问题,以便它直接调用
onHandleIntent
。因此,举例来说,如果您有一个 LocationUpdaterService,您可以创建一个假类来重写onStart
函数,如下所示:LocationUpdaterService
是 IntentService 的子类,因此当您编写测试,只需使用像这样的LocationUpdaterServiceFake
类}
现在,每当您调用
startService
时,它都会绕过 IntentService 中的线程代码,只调用您的onHandleIntent
函数This is a bit late, but I just struggled with this. You could solve this by creating a class that simply overrides the onStart of you service so it calls
onHandleIntent
directly. So for instance, if you have a LocationUpdaterService, you could create a fake class that overrides theonStart
function like this:LocationUpdaterService
is a subclass of IntentService, so when you write your tests, just use theLocationUpdaterServiceFake
class like this}
Now whenever you call
startService
, it will bypass the threading code in IntentService and just call youronHandleIntent
function我刚刚开始测试自己的
IntentService
,事实证明这有点令人头疼。仍在尝试解决问题,但对于您似乎没有收到对方法
onHandleIntent()
的调用的情况,(我不太了解junit< 背后的技术细节/code> 所以请原谅我使用的术语)这应该是因为一旦您对
startService
的调用返回,基于您的代码的测试框架实际上会拆除或结束测试方法。没有足够的时间来触发onHandleIntent
。我通过在测试用例中添加无限循环来验证上述理论 - 只有这样我才能在
onHandleIntent
中看到记录的日志语句。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 behindjunit
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 tostartService
returns. There is insufficient time foronHandleIntent
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.你只需要添加一个:
选择startService后面的
XXXX
,然后它会让线程进入onHandleIntent
方法。You just have to add a:
Choose the
XXXX
after the startService, then it will let the thread go into theonHandleIntent
method.在 Android Studio 1.1 中,使用运行/调试配置运行测试时 |扩展 IntentService 的任何被测单元代码 (UUT) 上的 Android 测试工具,ServiceTestCase.java (JUnit?) 代码不会调用 UUT 中的 onHandleIntent(Intent Intent) 方法。 ServiceTestCase仅调用onCreate,因此问题出在测试代码中。
在我的文件 smSimulatorTest.java 中:
此时,我正在测试框架中寻找通过 Intents 测试 UUT 的其他解决方案,因为这就是 IntentService 的实例化方式。
http://developer.android.com/reference/android/app/IntentService.html - 要使用它,请扩展 IntentService 并实现 onHandleIntent(Intent)。 IntentService 将接收 Intent,启动工作线程,并根据需要停止服务。
我和其他人一样,按照上述文档的指示将代码放入 onHandleintent() 中,但是,ServiceTestCase 仅测试上面显示的 onStart 和 onStartCommand。
In Android Studio 1.1, when running tests using the Run/Debug Configuration | Android Tests facility on any unit under test code (UUT) that extends IntentService, the ServiceTestCase.java (JUnit?) code does not call onHandleIntent(Intent intent) method in the UUT. ServiceTestCase only calls onCreate so the problem is in the test code.
In my file smSimulatorTest.java:
At this point, I'm looking for other solutions in the testing framework that test UUTs through Intents since this is how IntentService is instantiated.
http://developer.android.com/reference/android/app/IntentService.html - To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
I, like others, put my code in the onHandleintent() as directed by the above documentation, however, ServiceTestCase only tests onStart and onStartCommand has shown above.
这是我现在的方法:
调用服务的启动 Intent 指定要测试的 Service 方法
<前><代码>
公共无效test_can_do_the_work(){
意图startIntent = new Intent();
startIntent.putExtra("IN_TEST_MODE", "TEST_SPECIFIC_METHOD");
startIntent.setClass(getContext(), MyServiceToTest.class);
启动服务(启动意图);
断言NotNull(getService()); // 您的断言服务特定断言
}
在服务onStart中,我们检查是否传递了具体的Extra,并调用该方法进行测试。当处理意图触发时,这不会执行。
<前><代码>
@覆盖
公共无效onStart(Intent意图,int startId){
super.onStart(意图, startId);
String in_test_mode = Intent.getStringExtra("TEST_SPECIFIC_METHOD");
如果(in_test_mode!= null){
doServiceWork();
}
}
This is my approach for now:
The start Intent that invokes the service specifies the Service method to test
In the service onStart, we check for the specific Extra passed and call the method to test. This won't execute when Handle intent fired.