如何用Robolectric测试IntentService? [英] How to test an IntentService with Robolectric?
本文介绍了如何用Robolectric测试IntentService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Robolectric
测试IntentService
的onHandleIntent()
方法.
I'm trying to test the onHandleIntent()
method of an IntentService
using Robolectric
.
我从以下位置开始服务:
I'm starting the service with:
Activity activity = new Activity();
Intent intent = new Intent(activity, MyService.class);
activity.startService(intent);
ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedService();
assertNotNull(startedIntent);
像startedIntent
这样的
似乎不为空,但似乎未调用onHandleIntent()
.
我应该如何测试?
推荐答案
onHandleIntent
是受保护的方法,因此无法直接调用.
onHandleIntent
is a protected method so it can't be called directly.
我的解决方案是在测试用例中扩展服务类,覆盖onHandleIntent
使其公开并调用super.onHandleIntent(intent)
my solution was to extend the service class in my test case, override onHandleIntent
making it public and calling super.onHandleIntent(intent)
然后直接从测试用例中调用onHandleIntent
.
then call onHandleIntent
directly from the test case.
这篇关于如何用Robolectric测试IntentService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文