Using PendingIntent in unit test to test a service
I've got a IntentService
(actually a WakefulIntentService!) that returns result to requests (i.e., from activities) using a PendingIntent
that is added to the intent used to start the service. Earlier I used ResultReceiver
but found that it crashes if the device goes to sleep or is rotated while the request is being handled.
As unit test for this service I'm using the ServiceTestCase<Service>
class. That worked fine when using ResultReceiver
since it could be added to the intent passed to the service. However, ServiceTestCase
does not contain Activity's createPendingResult
method.
Therefore I wonder what's the best approach for testing this service. Should I use ActivityInstrumentationTestCase2 or ActivityUnitTestCase
instead for my unit test? Or any better ideas?
Btw, I read in the Android Dev. guide chapter on Services the following:
"However, if you want the service to se开发者_如何学Gond a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result."
Using broadcasts for a IntentService
that is local to the activities sounds like overkill to me, but I'll be happy to hear others' opinions on that.
精彩评论