我正在设计一个带有一些不太简单的类的系统,这些类需要一个Context对象才能对其进行初始化.这些类使用第三方类,它们也需要上下文初始化.该类还利用上下文加载功能所需的许多字符串资源.
问题在于为这些类编写仪表单元测试.当我尝试使用InstrumentationRegistry.getContext()获取用于测试的Context对象时,遇到了一个异常,即上下文找不到与该类关联的字符串资源(android.content.res.Resources $NotFoundException).
我的问题是:如何设计这些测试,以便上下文可以检索所需的字符串资源,还可以用作第三方类的合适上下文对象?我只能做很多模拟工作,因为其中一些类会处理auth令牌,这很难模拟.在Android域中,我不是唯一一个遇到此问题的人,所以我敢肯定,对于这个普遍存在的问题,有一个通用的解决方案.
编辑:
如建议的那样,我尝试将Robolectric(版本3.3.2)集成到我的项目中,但是,当我尝试运行单元测试时,遇到以下错误:
Error:Error converting bytecode to dex:Cause: Dex cannot parse version 52 byte code.This is caused by library dependencIEs that have been compiled using Java 8 or above.If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7'sourceCompatibility = '1.7'to that submodule's build.gradle file.
我试图将targetCompatibility和sourceCompatibility行添加到我的gradle文件中(在几个位置)无济于事.
这是我的手机build.gradle:
apply plugin: 'com.androID.application'apply plugin: 'checkstyle'apply plugin: 'io.fabric'project.ext { supportlibVersion = '25.3.0' multIDexSupportVersion = '1.0.1' gsonVersion = '2.8.0' retrofitVersion = '2.2.0' daggerVersion = '2.4' butterKnifeVersion = '8.5.1' eventBusversion = '3.0.0' awscoreServicesversion = '2.2.+' twitterKitVersion = '2.3.2@aar' facebookVersion = '4.+' crashlyticsversion = '2.6.7@aar' autovalueVersion = '1.2' autovalueParcelVersion = '0.2.5' autovalueGsonVersion = '0.4.4' permissiondispatcher = '2.2.0' testRunnerVersion = '0.5' espressoVersion = '2.2.2' junitVersion = '4.12' roboelectricVersion = '3.3.2'}def gitSha = exec('git rev-parse --short head', "unkNown");def gitCommitCount = 100 + Integer.parseInt(exec('git rev-List --count head', "-1"))def gitTag = exec('git describe --Tags', stringify(gitCommitCount))def gitTimestamp = exec('git log -n 1 --format=%at', -1)def appID = "com.example.myapp"def isCi = "true".equals(System.getenv("CI"))// Uncomment if you wish to enable Jack & Java8// apply from: 'jack.gradle'// Uncomment if you wish to enable Sonar//apply from: 'sonar.gradle'androID { compileSdkVersion 25 buildToolsversion "25.0.2" defaultConfig { applicationID appID minSdkVersion 16 targetSdkVersion 25 multIDexEnabled = true versionCode gitCommitCount versionname gitTag testInstrumentationRunner "androID.support.test.runner.AndroIDJUnitRunner" buildConfigFIEld 'String', 'GIT_SHA', "\"${gitSha}\"" buildConfigFIEld 'long', 'GIT_TIMESTAMP', "${gitTimestamp}L" } buildTypes { deBUG { applicationIDSuffix '.deBUG' } release { MinifyEnabled true proguardfiles getDefaultProguardfile('proguard-androID.txt'), 'proguard-rules.pro' } qa.initWith(buildTypes.release) qa { applicationIDSuffix '.qa' deBUGgable true } } lintoptions { abortOnError false } applicationVariants.all { variant -> def strictMode = !variant.name.equals("release") buildConfigFIEld 'boolean', 'STRICT_MODE_ENABLED', "${strictMode}" }}configurations.all { resolutionStrategy { force "com.androID.support:support-annotations:$supportlibVersion" force "com.squareup.okhttp3:okhttp:3.4.1" force "com.squareup:okio:1.9.0" force "com.Google.guava:guava:19.0" }}dependencIEs { compile "com.androID.support:appcompat-v7:$supportlibVersion" compile "com.androID.support:design:$supportlibVersion" compile "com.androID.support:recyclervIEw-v7:$supportlibVersion" compile "com.androID.support:cardvIEw-v7:$supportlibVersion" compile "com.androID.support:multIDex:$multIDexSupportVersion" compile "com.squareup.retrofit2:retrofit:$retrofitVersion" compile "com.squareup.retrofit2:converter-gson:$retrofitVersion" compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' compile "com.Google.dagger:dagger:$daggerVersion" annotationProcessor "com.Google.dagger:dagger-compiler:$daggerVersion" provIDed 'javax.annotation:Jsr250-API:1.0' compile 'com.github.bumptech.glIDe:glIDe:3.7.0' compile 'com.jakewharton.timber:timber:4.3.1' compile "com.jakewharton:butterknife:$butterKnifeVersion" annotationProcessor "com.jakewharton:butterknife-compiler:$butterKnifeVersion" compile "org.greenrobot:eventbus:$eventBusversion" annotationProcessor "org.greenrobot:eventbus:$eventBusversion" compile 'io.reactivex.rxjava2:rxandroID:2.0.0' deBUGCompile 'com.squareup.okhttp3:logging-interceptor:3.4.2' compile "com.Google.auto.value:auto-value:$autovalueVersion" annotationProcessor "com.Google.auto.value:auto-value:$autovalueVersion" compile "com.ryanharter.auto.value:auto-value-parcel-adapter:$autovalueParcelVersion" annotationProcessor "com.ryanharter.auto.value:auto-value-parcel:$autovalueParcelVersion" compile "com.github.hotchemi:permissionsdispatcher:$permissiondispatcher" annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:$permissiondispatcher" compile("com.crashlytics.sdk.androID:crashlytics:$crashlyticsversion") { transitive = true; } compile("com.twitter.sdk.androID:twitter:$twitterKitVersion") { transitive = true } compile "com.facebook.androID:facebook-android-sdk:$facebookVersion" compile "com.amazonaws:aws-androID-sdk-core:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-core:$awscoreServicesversion" compile "com.amazonaws:aws-androID-sdk-APIgateway-core:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-APIgateway-core:$awscoreServicesversion" compile "com.amazonaws:aws-androID-sdk-cognito:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-cognito:$awscoreServicesversion" compile "com.amazonaws:aws-androID-sdk-cognitoIDentityprovIDer:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-cognitoIDentityprovIDer:$awscoreServicesversion" compile "com.amazonaws:aws-androID-sdk-lambda:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-lambda:$awscoreServicesversion" compile "com.amazonaws:aws-androID-sdk-sns:$awscoreServicesversion" annotationProcessor "com.amazonaws:aws-androID-sdk-sns:$awscoreServicesversion" androIDTestCompile "junit:junit:$junitVersion" androIDTestCompile "com.androID.support.test:runner:$testRunnerVersion" androIDTestCompile "com.androID.support.test:rules:$testRunnerVersion" androIDTestCompile "com.androID.support.test.espresso:espresso-intents:$espressoVersion" androIDTestCompile "com.androID.support.test.espresso:espresso-core:$espressoVersion" androIDTestCompile "com.squareup.retrofit2:retrofit-mock:$retrofitVersion" androIDTestCompile "org.robolectric:robolectric:$roboelectricVersion" testCompile "junit:junit:$junitVersion" testCompile 'com.Google.truth:truth:0.30' testCompile 'org.hamcrest:hamcrest-all:1.3' testCompile "org.robolectric:robolectric:$roboelectricVersion"}task checkCodingStyle(type: Checkstyle) { description 'Runs Checkstyle inspection against AndroID sourcesets.' group = 'Code Quality' ignoreFailures = false showViolations = false source 'src' include '**/*.java' exclude '**/gen/**' exclude '**/R.java' exclude '**/BuildConfig.java' reports { xml.destination "$project.buildDir/reports/checkstyle/report.xml" } classpath = files() configfile = file("${rootProject.rootDir}/config/checkstyle/checkstyle.xml")}def stringify(int versionCode) { def builder = new StringBuilder(); def dot = "" String.format("%03d", versionCode).tochararray().each { builder.append(dot) builder.append(it) dot = "." } return builder.toString()}def exec(String command, Object fallback = null) { def cmd = command.execute([], project.rootDir) cmd.waitFor() if (cmd.exitValue() != 0) { if (fallback == null) { throw new RuntimeException("'$command' @R_502_5138@: $cmd.errorStream.text") } else { return fallback } } return cmd.text.trim()}if (isCi) { build.finalizedBy(checkCodingStyle)}
解决方法:
可接受的答案不是实际的解决方案.在很多情况下,您想测试与真正的AndroID框架的交互.就像其他存根一样,Robolectric可能会隐藏一些实际问题.
您的问题是您使用的InstrumentationRegistry.getContext()与您的应用程序使用的是不同的.根据文档:
Return the Context of this instrumentation’s package.
并且您应该使用InstrumentationRegistry.getTargetContext()代替:
Return a Context for the target application being instrumented.
因为它与第一个相反,将可以访问您的资源.
总结以上是内存溢出为你收集整理的Android Instrumented单元测试中的上下文和资源全部内容,希望文章能够帮你解决Android Instrumented单元测试中的上下文和资源所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)