Robolectric を使用して、応答としてインテントをブロードキャストする IntentService をテストするにはどうすればよいでしょうか?
次のクラスを想定しています。
class MyService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("action"));
}
}
私のテストケースでは、次のようなことをしようとしています:
@RunWith(RobolectricTestRunner.class)
public class MyServiceTest{
@Test
public void testPurchaseHappyPath() throws Exception {
Context context = new Activity();
// register broadcast receiver
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// test logic to ensure that this is called
}
};
context.registerReceiver(br, new IntentFilter("action"));
// This doesn't work
context.startService(new Intent(context, MyService.class));
}
}
MyService は、このアプローチを使用して開始されることはありません。私は Robolectric に比較的慣れていないので、明らかな何かが欠けている可能性があります。startService を呼び出す前に、何らかのバインディングを行う必要がありますか? コンテキストで sendBroadcast を呼び出すだけでブロードキャストが機能することを確認しました。何か案は?