繁体   English   中英

如何使用Robolectric测试IntentService?

[英]How to test an IntentService with Robolectric?

我想测试onHandleIntent()的方法IntentService使用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不为null,但似乎没有调用onHandleIntent()

我该怎么测试呢?

Robolectric有一个ServiceController ,可以像活动一样通过服务生命周期。 该控制器提供执行相应服务回调的所有方法(例如controller.attach().create().startCommand(0, 0).destroy() )。

从理论上讲,我们可以预期IntentService.onStartCommand()将通过其内部Handler触发IntentService.onHandleIntent(Intent) 但是这个Handler使用一个在后台线程上运行的Looper ,我不知道如何让这个线程前进到下一个任务。 解决方法是创建模仿相同行为的TestService ,但在主线程(用于运行测试的线程onHandleIntent(Intent)上触发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);
        }
    }
}

更新 :或者,为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;
    }
}

onHandleIntent是一个受保护的方法,因此无法直接调用它。

我的解决方案是在我的测试用例中扩展服务类,覆盖onHandleIntent使其公开并调用super.onHandleIntent(intent)

然后直接从测试用例中调用onHandleIntent

我认为你已经采取了错误的方式。 你在这里测试:

Intent startedIntent = shadowActivity.getNextStartedService();
assertNotNull(startedIntent);

startService()

然后,您必须假设Android已正确实现其结束,并且在调用startService()该服务确实将启动。

我想如果你想测试onHandleIntent()的行为,你必须直接调用它?

我不确定...但我认为你想要编写的测试只是测试Android框架。 你应该写两个测试。

1)测试是否调用了startService() (如您的示例所示)。

2)测试onHandleIntent()的行为(通过直接调用它)。

我对Robolectric经验很少,但希望这很有帮助。

干杯

我用这个

@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());
}

然后我运行测试,一切都很好

Robolectric 3.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问题buildIntentService修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM