我正在尝试使用onHandleIntent()
测试IntentService
的{{1}}方法。
我正在启动服务:
Robolectric
似乎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
。
我该如何测试?
答案 0 :(得分:11)
Robolectric的ServiceController
可以像活动一样通过服务生命周期。该控制器提供执行相应服务回调的所有方法(例如controller.attach().create().startCommand(0, 0).destroy()
)。
从理论上讲,我们可以预期IntentService.onStartCommand()
将通过其内部IntentService.onHandleIntent(Intent)
触发Handler
。但是这个Handler
使用在后台线程上运行的Looper
,我不知道如何让这个线程前进到下一个任务。解决方法是创建模仿相同行为的TestService
,但在主线程(用于运行测试的线程)上触发onHandleIntent(Intent)
。
@RunWith(RobolectricGradleTestRunner.class)
public class MyIntentServiceTest {
private TestService service;
private ServiceController<TestService> controller;
@Before
public void setUp() {
controller = Robolectric.buildService(TestService.class);
service = controller.attach().create().get();
}
@Test
public void testWithIntent() {
Intent intent = new Intent(RuntimeEnvironment.application, TestService.class);
// add extras to intent
controller.withIntent(intent).startCommand(0, 0);
// assert here
}
@After
public void tearDown() {
controller.destroy();
}
public static class TestService extends MyIntentService {
public boolean enabled = true;
@Override
public void onStart(Intent intent, int startId) {
// same logic as in internal ServiceHandler.handleMessage()
// but runs on same thread as Service
onHandleIntent(intent);
stopSelf(startId);
}
}
}
UPDATE :或者,为IntentService创建类似的控制器非常简单,如下所示:
public class IntentServiceController<T extends IntentService> extends ServiceController<T> {
public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) {
try {
return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass);
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
}
private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException {
super(shadowsAdapter, serviceClass);
}
@Override
public IntentServiceController<T> withIntent(Intent intent) {
super.withIntent(intent);
return this;
}
@Override
public IntentServiceController<T> attach() {
super.attach();
return this;
}
@Override
public IntentServiceController<T> bind() {
super.bind();
return this;
}
@Override
public IntentServiceController<T> create() {
super.create();
return this;
}
@Override
public IntentServiceController<T> destroy() {
super.destroy();
return this;
}
@Override
public IntentServiceController<T> rebind() {
super.rebind();
return this;
}
@Override
public IntentServiceController<T> startCommand(int flags, int startId) {
super.startCommand(flags, startId);
return this;
}
@Override
public IntentServiceController<T> unbind() {
super.unbind();
return this;
}
public IntentServiceController<T> handleIntent() {
invokeWhilePaused("onHandleIntent", getIntent());
return this;
}
}
答案 1 :(得分:6)
onHandleIntent
是受保护的方法,因此无法直接调用。
我的解决方案是在我的测试用例中扩展服务类,覆盖onHandleIntent
使其公开并调用super.onHandleIntent(intent)
然后直接从测试用例中调用onHandleIntent
。
答案 2 :(得分:1)
我认为你以错误的方式接近了这一点。 你在这里测试:
Intent startedIntent = shadowActivity.getNextStartedService();
assertNotNull(startedIntent);
startService()
被调用。
然后,您必须假设Android已正确实现其结束,并且在调用startService()
之后,确实会启动该服务。
我认为如果你想测试onHandleIntent()
的行为,你必须直接调用它吗?
我不确定......但我认为您要编写的测试只是测试Android框架。你应该写两个测试。
1)测试startService()
被调用(如您的示例所示)。
2)测试onHandleIntent()
的行为(通过直接调用)。
我对Robolectric
的经验很少,但希望这会有所帮助。
干杯
答案 3 :(得分:1)
我用这个
@Before
public void setup(){
mainActivity = Robolectric.buildActivity(MainActivity.class)
.create()
.start()
.resume()
.get();
context = Robolectric.getShadowApplication().getApplicationContext();
bt_start = (Button) mainActivity.findViewById(R.id.button);
bt_stop = (Button) mainActivity.findViewById(R.id.button2);
}
@Test
public void serviceIsOn(){
bt_start.performClick();
Intent intent = Robolectric.shadowOf(mainActivity).peekNextStartedService();
assertEquals(MiService.class.getCanonicalName(),intent.getComponent().getClassName());
}
然后我运行测试,一切正常
答案 4 :(得分:1)
创建服务实例
直接调用onHandleIntent
YourService svc = Robolectric.buildService(YourService.class).get();
Intent fakeIntent = new Intent();
fakeIntent.putExtra(YourService.SOME_EXTRA, "debug|fail");
svc.onHandleIntent(fakeIntent);
assertThat(something.happened).isEqualTo(true);
注意:即使您正在测试IntentService,也要使用Robolectric.buildService
(而不是.buildIntentService
)。据我所知,.buildIntentService
目前无效。
更新2017-05-17:
buildIntentService
的问题将为fixed in future