Я получил некоторые из моих старых тестов, работающих с новой функцией поддержки unit test для Android Studio 1.1.0.
При запуске gradlew testDebug тесты запускаются, но все тесты, требующие контекста, терпят неудачу, потому что 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);
}
}
и
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);
}
}
Это мой модуль build.gradle
:
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'
}
и здесь build.gradle
для проекта:
// 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()
}
}
EDIT: мои тесты все работали до обновления до AS 1.1.0 и запускались на устройстве/эмуляторе.
ИЗМЕНИТЬ:
Heres 2 скриншотов неудачных инструментов InstrumentationTestCase и AndroidTestCase: