6

IntentServiceAndroid が提供するServiceTestCaseクラスを使用してテストしようとしましたがtestServiceTestCaseSetUpProperly、そもそもテストが失敗して失敗します。私のコードは次のとおりです。

public class MyClientServiceTest 
   extends ServiceTestCase<MyClientService> {
    public static final String TAG = MyClientServiceTest.class.getName();
    public static final String FILE_PREFIX = "test_";

    private boolean bLoggingIn = false;
    private boolean bLoggingOut = false;
    private boolean bSendingScanRequest = false;
    private boolean bGettingTemplates = false;

    private final Class SERVICE_CLASS = MyClientService.class;

    private RenamingDelegatingContext rdContext = null;

    public MyClientServiceTest() {
        super(MyClientService.class);
    }

    public MyClientServiceTest(Class<MyClientService> serviceType) {
        super(MyClientService.class);   
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        rdContext = new RenamingDelegatingContext
                    (getSystemContext(), FILE_PREFIX);
        setContext(rdContext);
    }

    public void testThrowsExceptionOnNoActionPassedInIntent() {
        Intent intent = new Intent(rdContext, SERVICE_CLASS);
        Exception e = null;
        try{
            startService(intent);
        } catch(IllegalArgumentException ex) {
            e = ex;
        } finally {
            assertNotNull(e);
        }
    }

    public void testThrowsExceptionOnInvalidAction() {
        Intent intent = new Intent(rdContext, SERVICE_CLASS);
        intent.setAction("SurelyInvalidAction");
        Exception e = null;
        try {
            startService(intent);
        } catch(IllegalArgumentException ex) {
            e = ex;
        } finally {
            assertNotNull(e);
        }
    }
}

このテストを JUnit で実行しようとすると、次のようになります。

org.itay.mobile.test.MyClientServiceTest
testServiceTestCaseSetUpProperly(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at android.test.ServiceTestCase.setupService(ServiceTestCase.java:152)
at   android.test.ServiceTestCase.testServiceTestCaseSetUpProperly(ServiceTestCase.java:330)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

testThrowsExceptionOnInvalidAction(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at      org.itay.mobile.test.MyClientServiceTest.testThrowsExceptionOnInvalidAction(MyClientServiceTest.java:61)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

testThrowsExceptionOnNoActionPassedInIntent(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at   org.itay.mobile.test.MyClientServiceTest.testThrowsExceptionOnNoActionPassedInIntent(MyClientServiceTest.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

Android IntentServicesのテストが特に難しいことを収集したにもかかわらず、testServiceTestCaseSetUpProperlyテストが失敗していることは、( Android docsによると) 私のサービスが Context を適切に初期化できなかったことを意味します。それ、どうやったら出来るの?そうでない場合 ( setContext()inを使用して Context を初期化するためsetUp())、これを回避するにはどうすればよいでしょうか?

また、Android サービスのテストに関する一般的なガイドラインを提供するリンク (もちろん、実装例を含む) を誰かが教えてくれることを非常にありがたく思います。

4

0 に答える 0