I am trying to write a test test for my NetworkMonitorService , as described in the official documentation testing your service .
I am currently stuck because I canβt understand how I can get a link to a service I started to insert layouts into it and approve the behavior.
My code is:
@RunWith(AndroidJUnit4.class) @SmallTest public class NetworkMonitorServiceTest { @Rule public final ServiceTestRule mServiceTestRule = new ServiceTestRule(); @Test public void serviceStarted_someEventHappenedInOnStartCommand() { try { mServiceTestRule.startService(new Intent( InstrumentationRegistry.getTargetContext(), NetworkMonitorService.class)); } catch (TimeoutException e) { throw new RuntimeException("timed out"); }
The service in question does not support binding. I think that if I implemented binding support and then used this in a test to get a link to a service, it could work. However, I don't like writing production code just to support test cases ...
So, how can I test (test) a Service that does not support binding?
source share