2016-03-05 3 views
5

の内側に結合していないサービスを開始できません: http://developer.android.com/tools/testing-support-library/index.html私はテスト・サポート・ライブラリ・フレームワークの基本的な結合していないサービスをテストしようとしているのAndroidのJUnit 4インストルメントテスト

LocalService.java:

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

LocalServiceTest.java :私は、テストを実行しようとすると、私はTimeoutException次取得

@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); 
    } 
} 

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 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at android.support.test.rule.ServiceTestRule$ServiceStatement.evaluate(ServiceTestRule.java:329) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729) 

ServiceTestRuleでこの種のサービスをテストすることは可能ですか? いいえの場合は、JUnit 4テストクラスを使用してサービスを開始および停止する適切な方法がありますか?

+1

この同じ問題が発生しました。そして、解決策はonBindを実装してこのサービスへの参照を返すことでした。 – PSIXO

+0

はい、私は同じようにしました。残念ながら、この場合、サービスの初期化を待つためにsleep()を追加する必要がありました。とにかくありがとうございました! –

答えて

2

はこの1つ

@Ruleを 公共最終ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(60L、TimeUnit.SECONDS)試してみてください私のために働きました。

1

これは

@Test(timeout=1000 * 60) 
public void testWithStartedService(){ 
    try { 
     mServiceRule.startService(
       new Intent(InstrumentationRegistry.getTargetContext(), UpdaterService.class)); 
    } catch (TimeoutException e) { 
     e.printStackTrace(); 
    } 
    //do something 
} 
関連する問題