Android单元测试遇到的问题

Android 应用单元测试涉及 Telephony 环境初始化问题 因为在单元测试环境中,Android 框架和服务(如 SubscriptionManager)可能不会如预期那样初始化。应该如何注入Telephony模块相关类到测试环境? 阅读详情

使用的框架:Robolectric 3.0 or Espresso


1. FileNotFoundException: build\intermediates\bundles\debug\AndroidManifest.xml (系统找不到指定的路径。)

解决方法:编辑configuration,设置工作目录到对应的module即可


2. 错误: 无法访问AndroidHttpClient
找不到android.net.http.AndroidHttpClient的类文件


解决方法:从sdk\platforms\android-23\optional目录下拷贝org.apache.http.legacy.jar到工程中并加入依赖中即可


3. java.lang.RuntimeException: Method v in android.util.Log not mocked

解决方法:

testOptions {
    unitTests.returnDefaultValues = true
}


4. FileNotFoundException: build\intermediates\assets\debug\ap2d.data

通常只有测试模块的assets资源会在编译时放在build\intermediates\assets\debug,其他的如包含在jar或aar文件中的一般不会放在该目录,那么roboletric在查找文件时就找不到

解决方法:从其他地方拷贝到该目录即可,还有就是重写RobolectricGradleTestRunner.getAppManifest方法返回新的assetDir


5. 错误:IllegalArgumentException: input == null!
at javax.imageio.ImageIO.createImageInputStream(ImageIO.java:339)
at org.robolectric.shadows.ShadowBitmapFactory.getImageSizeFromStream(ShadowBitmapFactory.java:223)
at org.robolectric.shadows.ShadowBitmapFactory.decodeStream(ShadowBitmapFactory.java:104)
at org.robolectric.shadows.ShadowBitmapFactory.decodeStream(ShadowBitmapFactory.java:98)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java)

通常是由于传递给decodeStream的InputStream参数是由Resources.openRawResource返回的,且为空,为空是因为Resources对象未shadowed,故取不到资源


解决方法:使用BitmapFactory.decodeResource来加载图片


6. 测试时需要用到的公共类,放到一个module里,在需要引用的module中添加testCompile


7. 测试RxJava Observable时可以设置SubscribeOnScheduler和ObserverOnScheduler为Schedulers.immediate(),使一切同步进行,避免异步执行在不同线程可能导致的问题


8. 在测试使用OkHttp的网络调用时可以设置Intercepter来拦截请求,返回预设的值


9. 使用Expresso进行点击测试报如下错误:

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with string from resource id: <2131165384>[no_account_instant_register] value: 没有账号,立即注册'.
...
Caused by: android.support.test.espresso.PerformException: Error performing 'Send down montion event' on view 'unknown'.
...
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
...
Caused by: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
...

通常是由于键盘等遮住了目标View

详情见https://code.google.com/p/android/issues/detail?id=190169

解决方法:可能是关闭输入法


10. 在Espresso中读取文件数据

方法:

a. 在AndroidTest目录下建立以assets目录,将文件放到其中,该目录中的文件会被打包到测试apk中;

b. 调用InstrumentationRegistry.getContext().getAssets()获得AssetsManager,再通过AssetsManager来打开文件


11. 使用Espresso进行测试时,如果有异步操作,可以使用IdlingResource等待异步操作结束

参考见https://www.future-processing.pl/blog/android-testing-with-espresso-2-and-dagger-2-mocking-long-running-operations/


12. 改变Robolectric的版本到3.1遇到的问题

Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1.pom from repository sonatype at https://oss.sonatype.org/content/groups/public/
Error transferring file: oss.sonatype.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:pom:3.1' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: oss.sonatype.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1.pom from repository central at http://repo1.maven.org/maven2
Error transferring file: repo1.maven.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:pom:3.1' from repository central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1-21.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/
Error transferring file: oss.sonatype.org
Downloading: org/robolectric/shadows-core/3.1/shadows-core-3.1-21.jar from repository central at http://repo1.maven.org/maven2
Error transferring file: repo1.maven.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:jar:21:3.1' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: oss.sonatype.org
[WARNING] Unable to get resource 'org.robolectric:shadows-core:jar:21:3.1' from repository central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org

Unable to resolve artifact: Missing:
----------
1) org.robolectric:shadows-core:jar:21:3.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
  1) org.apache.maven:super-pom:pom:2.0
  2) org.robolectric:shadows-core:jar:21:3.1

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2),
  sonatype (https://oss.sonatype.org/content/groups/public/)

at org.apache.maven.artifact.ant.DependenciesTask.doExecuteResolution(DependenciesTask.java:268)
at org.apache.maven.artifact.ant.DependenciesTask.doExecute(DependenciesTask.java:168)
at org.apache.maven.artifact.ant.AbstractArtifactTask.execute(AbstractArtifactTask.java:751)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrls(MavenDependencyResolver.java:52)
at org.robolectric.internal.dependency.CachedDependencyResolver.getLocalArtifactUrls(CachedDependencyResolver.java:43)
at org.robolectric.internal.InstrumentingClassLoaderFactory.getSdkEnvironment(InstrumentingClassLoaderFactory.java:43)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:190)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException: Missing:
----------
1) org.robolectric:shadows-core:jar:21:3.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.1 -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency: 
  1) org.apache.maven:super-pom:pom:2.0
  2) org.robolectric:shadows-core:jar:21:3.1

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2),
  sonatype (https://oss.sonatype.org/content/groups/public/)

at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolveTransitively(DefaultArtifactResolver.java:360)
at org.apache.maven.artifact.ant.DependenciesTask.doExecuteResolution(DependenciesTask.java:263)
... 23 more

原因:网络问题

解决方法:多次尝试,或找一个好的VPN

基于FreeSwitch与SIP UA的蓝牙电话坐席系统实战部署 本文详细介绍了如何基于FreeSwitch开源软交换平台,结合SIP UA软电话与蓝牙电话App,在局域网内搭建一套低成本、自主可控的电话坐席系统。文章提供了从环境准备、FreeSwitch安装配置、用户与拨号计划创建,到SIP软电话测试和蓝牙设备接入的完整实战部署流程,并给出了关键优化与排坑指南,适合小微团队或个人开发者构建内部通讯工具。 阅读详情

相关推荐

Sentinel2 哨兵2数据下载方法(USGS)-史上最全讲解

本文讲述了从USGS官网下载Sentinel2的方法。主要包括USGS官网的注册和下载流程,以及选定研究区中所出现的问题、解决办法。

xiatiandexia123的博客 6万+

app联调出现java.lang.SecurityException问题的解决方法

最近在梳理以前做项目时记的笔记。当时开发的一个模块涉及到与搜索模块的联调,通过联调实现用搜索功能实现跳转自己开发的模块中的几个Activity,但在联调过程中,报出了:java.lang.SecurityException的问题。 根据出现的错误找到原因:通过在被调用的Activty中的Androidmanifest中添加 android:exported="true" 问题得以解决,先通过本文记录一下。 注:后续的安全性问题未考虑,关于这个问题后面还需要研究。 ...

ambitionLlll的博客 5020

3款 433MHz 收发模块对比:SYN480R vs RXB6 vs DL-TX19,实测卷帘门遥控距离

本文深度评测了3款433MHz收发模块SYN480R、RXB6与DL-TX19在卷帘门遥控距离、穿透性能和抗干扰能力等方面的表现。通过实测数据对比,分析了超外差、超再生和PLL锁相环技术的优劣,为智能家居和工业控制领域的无线遥控方案选型提供专业参考。

weixin_30700977的博客 393

SecurityException: Injecting input events requires the caller to have the INJECT_EVENTS permission

第一,在开发者选项中,打开【USB 调试(安全设置)】。打开有一定风险,系统会确认多遍。第二,是USB连接上后,一定要选择文件传输,否则还是有错误。以上方法,在 红米 K60 + 澎湃 1.0 上亲测有效。

小小木屋 1436

空天大模型详解

空天大模型是指面向空天信息领域(如卫星遥感、导航、气象等)设计的多模态人工智能模型,能够处理海量遥感数据、多传感器信息及跨时空分析任务。多模态融合:整合可见光、雷达、热红外、多光谱等多源遥感数据,构建统一表征空间,例如“空天·灵眸”3.0版支持航空、卫星及无人机数据的自动化解译。轻量化与高效推理:针对星载计算场景,采用分层量化、跨模态蒸馏技术压缩模型参数(如JigonGPT从1.02B压缩至0.23B,性能仅牺牲5.2%)18,并通过热传导架构提升计算速度2.4倍。神经符号结合。

zxy98的专栏 1566

MTK手机传感器驱动开发避坑指南:从SCP代码大小限制到Overlay加载流程详解

本文详细解析了MTK平台传感器驱动开发中的常见问题,包括SCP代码大小限制、Overlay加载流程及客制化传感器集成。通过实战案例和优化策略,帮助开发者高效解决MTK传感器驱动开发中的技术难题,提升开发效率。

weixin_42522374的博客 401

aosp14安卓14无法事件注入报错was not directed at a window owned by uid

既然上面分析的Instrumentation已经不行了,也不太可能修改framework把Instrumentation代码修改或者扩展,这里就使用和scrcpy的方法一样,采用InputManager接口,但是接口又是hide的,可以和scrcpy一样采用反射方式,我这边给出一种新方法,不采用反射,用的是一个模拟假接口,类和接口和InputManager一模一样,但是空实现。明显看到这里居然报错了,,,明明android 10版本是好的,那这个是为啥?所以可以排除掉没有权限的问题,那么为啥还会报错呢?

liaosongmao1的专栏 776

基于LabVIEW控制仪器检测与监控 --普源示波器DS1102Z-E

LabVIEW 控制普源示波器 代码实现与解释

s0101100的博客 4231

小米手机adb命令报错

设置>>更多设置>>开发者选项>>开启USB调试及USB调试(安全设置),并在系统跟踪中关闭跟踪。打开开发者选项 ,小米手机是点进我的设备,进入全部参数然后连续点击系统版本号进入开发者模式。

Fyl0525的博客 3318

嘉立创EDA实战:从点亮第一盏灯到完成首块PCB

本文详细介绍了使用嘉立创EDA从点亮第一盏LED灯到完成首块PCB的全过程。通过实战案例分享,包括原理图绘制、PCB设计、DRC检查、覆铜操作等关键步骤,帮助电子设计爱好者快速掌握嘉立创EDA的使用技巧,并避免常见的设计错误。

weixin_30314793的博客 372

RagFlow专题三、RagFlow 关键技术(向量数据库、文档分块、Prompt 设计与召回排序优化)

FAISS 是由 Meta(Facebook)开源的高效向量检索库,适用于大规模向量搜索任务。,并了解了如何通过 RagFlow 机制优化信息检索和生成质量。(FAISS、Milvus、ES)用于高效存储和检索语义向量。提高 LLM 生成效果,增强 RagFlow 任务质量。(分块)策略,将文档拆分成更小的片段,提高检索效率。提高检索精准度,避免文本过长导致信息丢失。提升检索结果的相关性,提高生成质量。当多个检索结果被返回时,我们需要一个。在前一篇文章中,我们详细探讨了。在 RagFlow 任务中,

xiaoqi270620903的专栏 3040

出现java.lang.SecurityException:异常处理方法

C:\Users\Admin\.jdks\openjdk-16.0.2\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=51019:D:\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\project\out\production\test java.test01.Dog1 Error: A JNI error has occurred, pl

weixin_42474086的博客 1万+

【AIGC】Qwen3-Embedding:Embedding与Rerank模型新标杆

Qwen3-Embedding:文本嵌入与重排序模型的技术解析与应用实践

一个内容严谨、方向前沿、极具实践价值的AI大模型技术分享站 3089

android单元测试遇到的问题

从《大话企业级android开发》抄的例子,看不太懂,尝试着运行了一下,报错:ClassNotFoundException:android.test.AndroidTestCase Manifest文件内容:     package="com.example.unittestandlog"     android:versionCode="1"     android:versio

kaitlyn2011的专栏 637

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

android单元测试方法,关于Android单元测试的几个重要问题

前言已经一个月没写文章了,由于9月份在plan国庆旅行计划,国庆前前后后去了14天旅行,所以没时间写,哈哈。言归正传,上一篇文章《Android单元测试 - 如何开始?》介绍了几款单元测试框架、Junit & Mockito基本用法、依赖隔离 & Mock概念,本篇主要解答单元测试中几个重要问题。在单元测试交流微信群,很多新进来的小伙伴,都会几个大同小异的问题。我们几个老鸟们答完一...

weixin_39601511的博客 396

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

安卓端开发和单元测试遇到的问题汇总

问题1:无法加载想要加载的文件 原因:安卓端编写单元测试时只拥有该程序内的文件系统访问权限,可以将需要用到的文件放到一下路径下,方便访问。 val appContext = InstrumentationRegistry.getInstrumentation().targetContext val path = "${appContext.getExternalFilesDir("")?.absolutePath}" 问题2:使用Junit4进行单元测试时,断言某单元测试方法必定报错,想跳过它怎么办 可以

weixin_44472370的博客 2189

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Android关键方法,Android单元测试 - 几个重要问题

前言已经一个月没写文章了,由于9月份在plan国庆旅行计划,国庆前前后后去了14天旅行,所以没时间写,哈哈。言归正传,上一篇文章《Android单元测试 - 如何开始?》介绍了几款单元测试框架、Junit & Mockito基本用法、依赖隔离 & Mock概念,本篇主要解答单元测试中几个重要问题。在单元测试交流微信群,很多新进来的小伙伴,都会几个大同小异的问题。我们几个老鸟们答完...

weixin_34470566的博客 430

android studio单元测试 class not found:_Android单元测试常见问题和套路

点击上方蓝字关注??来源:老王头碎碎念https://www.jianshu.com/p/ab9e21cdeb13前言最近开始给公司的项目写单元测试,先从已经抽离成库的类开始写,因为不会涉及到界面,所以目前写起来较为容易。单元测试框架采用的是 powerMock 框架,不过在写的过程中,发现了一些问题,今天我们就说一下一些常见的问题,和对某些类型代码常见的套路(针对这种类型应该如何编...

weixin_36341727的博客 1064

在Android中进行单元测试遇到的问题

问题1、Cannot connect to VM  socket closed 在使用JUnit进行测试的时候,遇到这个问题。网上的解释是:使用Eclipse对Java代码进行调试,无论是远程JVM还是本地JVM都会进行Socket通讯.发生这样的错误是由于这些软件会修改winsock,还会监听和占用一些端口,Socket通讯不上造成的。 我通过cmd →ping localhos

tianxiagongzheng的专栏 724

Android单元测试网络请求问题

Android通过单元测试网络请求问题 单元测试中想使用okhttp但是发现okhttp回调方法不执行,发现网络请求是异步请求原因,导致断点不能拦截, 解决办法需要调用okhttp的同步请求方式进行网络请求,然后单元测试时选择debug模式执行,运行速度相对慢一些,可以得到Response。 下面是示例: 代码比较老,用的是对okhttp进行封装的库: https://github.com/hongyangAndroid/okhttputils 这个库的同步请求和异步请求查看git中的文档,execute中

yxf0448的博客 714

如何运行 Android 应用测试?(单元测试&UI仪器测试问题总结)

介绍Android 单元测试的方法实现,以及使用 Android Studio 运行单元测试并查看测试覆盖率和报告的步骤,结合代码 @VisibleForTesting 标识可单侧的接口。

。 1919

Android单元测试---常见问题和套路

前言 最近开始给公司的项目写单元测试,先从已经抽离成库的类开始写,因为不会涉及到界面,所以目前写起来较为容易。 单元测试框架采用的是 powerMock 框架,不过在写的过程中,发现了一些问题,今天我们就说一下一些常见的问题,和对某些类型代码常见的套路(针对这种类型应该如何编写单元测试用例)。 常见问题 1、android 类方法调用为空,Method isEmpty ...

DoubleCake的专栏 1113

【Android-UnitTest】Android单元测试问题汇总

1. 基本使用 2. 外部类库引用出错 报错: java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details. at android.util.Log.d(Log.java) at ble.ztj.pro.controlble.ExampleUnitTest.addition_isCorrect(.

dandelionela的博客 1190

Android单元测试 - 几个重要问题

原文链接:http://www.jianshu.com/p/f5d197a4d83a 前言 已经一个月没写文章了,由于9月份在plan国庆旅行计划,国庆前前后后去了14天旅行,所以没时间写,哈哈。 言归正传,上一篇文章《Android单元测试 - 如何开始?》介绍了几款单元测试框架、Junit & Mockito基本用法、依赖隔...

weixin_33885253的博客 446

Android studio 单元测试无法运行的问题解决

Android studio 单元测试无法运行的问题解决 今天新换了笔记本,安装好环境后,发现项目的单元测试无法通过,报错信息如下: java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1 问题原因:AS安装后默认jdk是Android default jdk,如果本地的jdk和as默认的不一致就会导致无法单元测试 解决办法: 打开project structure

攀山望月 2826
上一篇: Format —— Java & Android
下一篇: Android开发中遇到的问题
misea
博客等级 码龄19年 6粉丝 22原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值