To simplify my question: I have a Service
that is invoked by startService
(rather then bindService
), does some calculation and publishes a method to some other component when it finishes.
I want to test its behavior. I have a Test class with a ServiceTestRule
and a test method that starts the service using:
serviceRule.startService(
new Intent(InstrumentationRegistry.getContext(), MyService.class));
I would like to add something like this:
mockedPublisher = Mockito.mock(Publisher.class) // Publisher is a member in MyService
service.setPublisher(mockedPublisher)
verify(mockedPublisher).publish(eq(correctResult))
But not quote sure where (in the @Test
method or @Before
?) and how to get the instance of the service itself in order to mock methods or inject mocked objects.
I saw some (very few) examples of how to use ServiceTestRule
but they all focus on binding services which are easier because you have a 'service object' in hand that you can at least mock or verify. With startService
I seem to have no affect on how the service in created and can't alter it.
Any help?