6

I have been trying to test my IntentService using the Android provided ServiceTestCase class, but the tests fail with the testServiceTestCaseSetUpProperly failing in the first place. My code follows:

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

What happens when I try to run this test with JUnit is this:

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)

Even though I have gathered that testing Android IntentServices especially daunting, the testServiceTestCaseSetUpProperly test is failing which means(according to Android docs) that my service failed to initialize a Context properly. How do I do that? If that is not the case (as I do initialize a Context using setContext() in setUp()), what can be done to work around this?

Also, I would highly appreciate someone pointing me to a link that provides general guidelines to Android service testing(with example implementation, of course).

Community
  • 1
  • 1
yati sagade
  • 1,355
  • 9
  • 24
  • No I haven't. You mean I should do a setContext(new MockContext()) in setUp()? Or am I missing something? – yati sagade Aug 13 '12 at 10:55
  • protected void setUp() throws Exception { super.setUp(); setContext(new MockContext()); } – Yahor10 Aug 13 '12 at 11:01
  • doesn't help, testServiceTestCaseSetUpProperly fails with the same error (though I had to use a MockContext subclass with an overridden getPackageName()) – yati sagade Aug 13 '12 at 11:08
  • I have had success calling AndroidTestCase's getContext() when create an intent for a service testcase, and not setting the context manually. Does your context need to specifically be a RenamingDelegatingContext? If you don't set it manually and breakpoint after setup, what does your context look like? – Billy Lazzaro Apr 22 '15 at 20:49
  • possible duplicate of [how to JUnit test IntentService](http://stackoverflow.com/questions/11923181/how-to-junit-test-intentservice) – rds May 21 '15 at 15:54
  • @yatisagade Hey, have you found any answer for this? Even my _testServiceTestCaseSetUpProperly_ failed. – Prudhvi Aug 12 '15 at 15:30

0 Answers0