小编Ram*_*dny的帖子

无法在Android JUnit 4检测测试中启动未绑定服务

我正在尝试使用测试支持库框架测试基本的未绑定服务: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)

android android-testing

6
推荐指数
2
解决办法
1455
查看次数

标签 统计

android ×1

android-testing ×1