title: Getting a Context for use in AndroidTestCase when class under test is not an activity
body: <p>I am trying to test using AndroidTestCase. I am trying to test only one particular class in my application, however this class does not extend Activity, Service or anything else. It is basically a plain Java class apart from the fact that it requires a Context. It is a pojo and some of its variables are objects that require android api calls in their creation, e.g. a call to the SensorManager.
  I tried to use:</p>
  
  <pre><code>Context context = getContext();
  </code></pre>
  
  <p>When running my tests this gives me the exception "System services not available to activites before onCreate()". Does that method have to be overridden? <code>
  final Context context = new IsolatedContext(null, getContext())</code> gives the same thing.</p>
  
  <p>The reason I am using the Android testing framework and not something like Robolectric is because the class I'm testing gathers hardware information about a device and so I want to run the tests on an actual device. I have looked at the developer docs for AndroidTestCase but can't see what I'm looking for in the examples. I'm not sure the other test case classes will achieve what I want. Any ideas? </p>
  
  <p>My test class:</p>
  
  <pre><code>public class DeviceTest extends AndroidTestCase {
  ClassToTest mClassToTest;
  
  @Override
  protected void setUp() throws Exception {
  final Context context = new IsolatedContext(null, getContext()) {
          @Override
          public Object getSystemService(final String pName) {
              return getContext().getSystemService(pName);
          }
      };
  
  mClassToTest = new ClassToTest(context);
   super.setUp();
  }
  
  public void testClassMethod() {
   Object mObject;
   mObject = mClassToTest.getObject();
   assertNotNull(mObject);
  }
  
  @Override
  protected void tearDown() throws Exception {
   mClassToTest = null;
   super.tearDown();
  }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE:</strong> After changing my setup to the following:</p>
  
  <pre><code> @Override
   protected void setUp() throws Exception {
   super.setUp();
   context = this.getContext();
   mClassToTest = new ClassToTest(context);
  }
  </code></pre>
  
  <p>I am getting an error that context is null. In what scenarios would AndroidTestCase.getContext() return null? My setup seems to be ok.... </p>