title: How to test an IntentService using a ServiceTestCase?
body: <p>I have been trying to test my <code>IntentService</code> using the Android provided <code>ServiceTestCase</code> class, but the tests fail with the <code>testServiceTestCaseSetUpProperly</code> failing in the first place. My code follows:</p>
  
  <pre><code>public class MyClientServiceTest 
     extends ServiceTestCase&lt;MyClientService&gt; {
      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&lt;MyClientService&gt; 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);
          }
      }
  }
  </code></pre>
  
  <p>What happens when I try to run this test with JUnit is this:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Even though I have gathered that testing Android IntentServices <a href="https://stackoverflow.com/questions/6667087/android-servicetestcase-for-intentservice">especially daunting</a>, the <code>testServiceTestCaseSetUpProperly</code> test is failing which means(according to <a href="http://developer.android.com/tools/testing/service_testing.html" rel="nofollow noreferrer">Android docs</a>) that my service failed to initialize a Context properly. How do I do that? If that is not the case (as I <em>do</em> initialize a Context using <code>setContext()</code> in <code>setUp()</code>), what can be done to work around this?</p>
  
  <p>Also, I would highly appreciate someone pointing me to a link that provides general guidelines to Android service testing(with example implementation, of course).</p>