我使用Android Studio 1.1.0的新单元测试支持功能运行了一些旧的测试.运行gradlew testDebug时,运行测试,但所有需要Context失败的测试都因为getContext(AndroidTestCase)/ getInstrumentation.getContext()(InstrumentationTestCase)都返回null.
我怎么解决这个问题?
这是我试过的两个变种:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
和
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模块build.gradle:
apply plugin: …Run Code Online (Sandbox Code Playgroud)