title: Getting context in AndroidTestCase or InstrumentationTestCase in Android Studio's Unit Test feature
body: <p>I got some of my old tests running with Android Studio 1.1.0's new unit test support feature. 
  When running gradlew testDebug the tests are run, but all of the tests that require a Context fail because <code>getContext</code> (AndroidTestCase) / <code>getInstrumentation.getContext()</code> (InstrumentationTestCase) both return null. </p>
  
  <p>How can I solve this? </p>
  
  <p>Here's two variants I've tried: </p>
  
  <pre><code>import android.content.Context;
  import android.test.InstrumentationTestCase;
  
  public class TestTest extends InstrumentationTestCase {
  
      Context context;
  
      public void setUp() throws Exception {
          super.setUp();
  
          context = getInstrumentation().getContext();
  
          assertNotNull(context);
  
      }
  
      public void testSomething() {
  
          assertEquals(false, true);
      }  
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import android.content.Context;
  import android.test.AndroidTestCase;
  
  public class TestTest extends AndroidTestCase {
  
      Context context;
  
      public void setUp() throws Exception {
          super.setUp();
  
          context = getContext();
  
          assertNotNull(context);
  
      }
  
      public void testSomething() {
  
          assertEquals(false, true);
      }
  
  }
  </code></pre>
  
  <p>This is my module's <code>build.gradle</code>: </p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 22
      buildToolsVersion "22.0.0"
  
      testOptions {
          unitTests.returnDefaultValues = true
      }
  
      defaultConfig {
          applicationId "com.example.test.penistest"
          minSdkVersion 15
          targetSdkVersion 22
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:22.0.0'
      testCompile 'junit:junit:4.12'
  }
  </code></pre>
  
  <p>and here the <code>build.gradle</code> for the project:</p>
  
  <pre><code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
  
  buildscript {
      repositories {
          jcenter()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:1.1.3'
  
          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
      }
  }
  
  allprojects {
      repositories {
          jcenter()
      }
  }
  </code></pre>
  
  <p>EDIT: My tests all worked before upgrading to AS 1.1.0 and ran on a device / emulator.</p>
  
  <p>EDIT: </p>
  
  <p>Heres 2 screenshots of failing InstrumentationTestCase and AndroidTestCase: </p>
  
  <p><img src="https://i.stack.imgur.com/whx66.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/7Nguj.png" alt="enter image description here"></p>