我正在尝试使用测试支持库框架测试基本的未绑定服务:http: //developer.android.com/tools/testing-support-library/index.html
LocalService.java:
public class LocalService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
LocalServiceTest.java:
@RunWith(AndroidJUnit4.class)
public class LocalServiceTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testWithUnboundService() throws TimeoutException {
Intent serviceIntent =
new Intent(InstrumentationRegistry.getTargetContext(), LocalService.class);
mServiceRule.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行测试时,我得到以下TimeoutException:
java.util.concurrent.TimeoutException: Waited for 5 SECONDS, but service was never connected
at android.support.test.rule.ServiceTestRule.waitOnLatch(ServiceTestRule.java:258)
at android.support.test.rule.ServiceTestRule.bindServiceAndWait(ServiceTestRule.java:207)
at android.support.test.rule.ServiceTestRule.startService(ServiceTestRule.java:137)
at com.example.android.testing.ServiceTestRuleSample.LocalServiceTest.testWithBoundService(LocalServiceTest.java:71)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at …Run Code Online (Sandbox Code Playgroud)