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).