当运行gradlew testDeBUG时,运行测试,但是由于getContext(AndroIDTestCase)/ getInstrumentation.getContext()(InstrumentationTestCase))都返回null,所以需要Context的所有测试都会返回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() }}
编辑:我的测试在升级到AS 1.1.0之前都运行,并运行在设备/模拟器上.
编辑:
Heres 2截图失败的InstrumentationTestCase和AndroIDTestCase:
@H_403_27@解决方法 更新 – 请使用Espresso编写仪器测试较新的例子:
我得到这些工作,而不部署到设备.将测试放在/ src / main / test /文件夹中.
这是一个较新的例子,我把你的例子和我们自己的临时测试项目测试了.我通过命令行运行测试:./gradlew clean test.请在这里阅读更多:https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support.
顶级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() }}
应用程式build.gradle:
apply plugin: 'com.androID.application'androID { compileSdkVersion 22 buildToolsversion "22.0.0" defaultConfig { applicationID "com.test" minSdkVersion 9 targetSdkVersion 22 versionCode 1 versionname "1.0" } buildTypes { release { MinifyEnabled false proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.pro' } } testoptions { // <-- You need this unitTests { returnDefaultValues = true } }}dependencIEs { compile filetree(dir: 'libs',include: ['*.jar']) compile 'com.androID.support:appcompat-v7:22.0.0' testCompile 'junit:junit:4.12' // <-- You need this}
基本测试:
InstrumentationTestCaseTest测试上下文和断言.
import androID.content.Context;import androID.test.InstrumentationTestCase;import androID.test.mock.MockContext;public class InstrumentationTestCaseTest extends InstrumentationTestCase { Context context; public voID setUp() throws Exception { super.setUp(); context = new MockContext(); assertNotNull(context); } public voID testSomething() { assertEquals(false,true); }}
ActivityTestCase来测试你的资源.
import androID.content.Context;import androID.content.res.Resources;import androID.test.ActivityTestCase;public class ActivityTestCaseTest extends ActivityTestCase { public voID testFoo() { Context testContext = getInstrumentation().getContext(); Resources testRes = testContext.getResources(); assertNotNull(testRes); assertNotNull(testRes.getString(R.string.app_name)); }}
AndroIDTestCase测试上下文和断言.
import androID.content.Context;import androID.test.AndroIDTestCase;import androID.test.mock.MockContext;public class AndroIDTestCaseTest extends AndroIDTestCase { Context context; public voID setUp() throws Exception { super.setUp(); context = new MockContext(); setContext(context); assertNotNull(context); } // Fake Failed test public voID testSomething() { assertEquals(false,true); }}
谷歌老例子:
在Googling之后,这个错误很多,我相信你的赌注是使用getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res).或类似的东西,以测试您的资源.
使用InstrumentationTestCase:
测试资源:
public class PrintoutPullParserTest extends InstrumentationTestCase { public voID testParsing() throws Exception { PrintoutPullParser parser = new PrintoutPullParser(); parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration)); }}
资料来源:https://stackoverflow.com/a/8870318/950427和https://stackoverflow.com/a/16763196/950427
使用ActivityTestCase:
测试资源:
public class Test extends ActivityTestCase { public voID testFoo() { // .. test project environment Context testContext = getInstrumentation().getContext(); Resources testRes = testContext.getResources(); inputStream ts = testRes.openRawResource(R.raw.your_res); assertNotNull(testRes); } }
资料来源:https://stackoverflow.com/a/9820390/950427
使用AndroIDTestCase:
获取上下文(一个简单的黑客):
private Context getTestContext() { try { Method getTestContext = ServiceTestCase.class.getmethod("getTestContext"); return (Context) getTestContext.invoke(this); } catch (final Exception exception) { exception.printstacktrace(); return null; }}
资料来源:https://stackoverflow.com/a/14232913/950427
但是,如果您查看AndroIDTestCase的源代码,您需要自己设置一个上下文:
资料来源:http://alvinalexander.com/java/jwarehouse/android/core/java/android/test/AndroidTestCase.java.shtml
@H_403_27@ @H_403_27@ 总结以上是内存溢出为你收集整理的在Android Studio的单元测试功能中获取AndroidTestCase或InstrumentationTestCase中的上下文全部内容,希望文章能够帮你解决在Android Studio的单元测试功能中获取AndroidTestCase或InstrumentationTestCase中的上下文所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)