2

I am being faced with this issue, basically the ServiceTestRule throws a TimeoutException when startService is called in an unbound services, and would like to know if someone knows of a possible workaround until a fix is released?

The unbound service class:

public class UnboundService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

The test class:

@RunWith(AndroidJUnit4.class)
@MediumTest
public class UnboundServiceTest {
    @Rule
    public final ServiceTestRule serviceRule = new ServiceTestRule();

    @Test
    public void worksAsStartedService() throws TimeoutException {
        Intent intent = new Intent(InstrumentationRegistry.getTargetContext(), UnboundService.class);
        serviceRule.startService(intent);
    }
}
Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67

1 Answers1

-1

Try this:

    @Rule
public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(60L, TimeUnit.SECONDS);
Blindsurfer
  • 363
  • 1
  • 3
  • 9
  • The problem is not related to the timed out but that the implementation of `startService` is trying to "bind to the started service to guarantee its started and connected before test execution". Check the way `ServiceTestRule` was implemented. They call `bindServiceAndWait`, but as it is an unbound service it never binds. – Luís Ramalho Jan 31 '17 at 12:52