Android 模拟器上的 Mockito

2024-04-19

Android 新手尝试在 Android 中使用我最喜欢的 Java 测试工具。我正在尝试使用 Mockito 1.9.5,如以下博客文章中所述,但无法在我的模拟器上运行测试(我目前没有用于测试的物理设备)。

Mockit-Android 教程:http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/ http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/

我能够毫无问题地执行所有正常的 Junit 测试,但是我利用 Mockito 进行的任何测试都会收到以下信息:

Can't open dex cache '/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex': No such file or directory
Unable to open or create cache for /data/data/com.trendium.peg/cache/Generated-621101.jar (/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex)
failed: testStartable(com.trendium.peg.services.RatingServiceTest)
java.lang.AssertionError: java.lang.ClassNotFoundException: RemoteRestTask_Proxy in loader dalvik.system.DexClassLoader@40a4b610

我已经做了很多谷歌搜索这些异常,重建了我的模拟器,重建了项目,以及许多其他各种想法,但没有运气解决这个问题。

旁注:我的目标是 SDK 11 及更高版本,怀疑这是否会产生影响,但值得注意。我现有的不利用 Mockito 的单元测试在同一测试运行中运行没有问题(28/28 运行,7 次失败)。

对 LogCat 的进一步分析表明,mockito cglib 显然不在类路径上,但是我目前不确定下一步:

03-25 09:10:42.990: W/dalvikvm(411): Unable to resolve superclass of Lorg/mockito/cglib/transform/AbstractProcessTask; (637)
03-25 09:10:43.000: W/dalvikvm(411): Link of class 'Lorg/mockito/cglib/transform/AbstractProcessTask;' failed
03-25 09:10:43.029: D/dalvikvm(411): GC_CONCURRENT freed 618K, 9% free 9226K/10055K, paused 4ms+6ms
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Cannot load class. Make sure it is in your apk. Class name: 'org.mockito.cglib.transform.AbstractProcessTask'. Message: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): java.lang.ClassNotFoundException: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.Class.classForName(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.Class.forName(Class.java:234)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:89)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:40)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:51)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:48)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.SimpleCache.get(SimpleCache.java:31)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:73)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:356)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3550)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.app.ActivityThread.access$2200(ActivityThread.java:123)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.os.Looper.loop(Looper.java:126)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.reflect.Method.invokeNative(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.reflect.Method.invoke(Method.java:491)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at dalvik.system.NativeStart.main(Native Method)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Caused by: java.lang.NoClassDefFoundError: org.mockito.cglib.transform.AbstractProcessTask
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  ... 26 more
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411): Caused by: java.lang.ClassNotFoundException: org.mockito.cglib.transform.AbstractProcessTask in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.example.mine.test-1.apk:/data/app/com.example.mine-1.apk]
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
03-25 09:10:43.040: W/ClassPathPackageInfoSource(411):  ... 26 more

根据此错误消息,我查看了 Mockito 源代码并注意到它正在使用 Ant 的任务。但是,我不认为 Ant 是 Mockito 的依赖项......

https://fisheye2.atlassian.com/browse/mockito/trunk/cglib-and-asm/src/org/mockito/cglib/transform/AbstractProcessTask.java?r=1430 https://fisheye2.atlassian.com/browse/mockito/trunk/cglib-and-asm/src/org/mockito/cglib/transform/AbstractProcessTask.java?r=1430

进一步调试,我再次被以下内容阻止:

03-25 15:07:01.726: I/dalvikvm(703): Failed resolving Lorg/junit/internal/AssumptionViolatedException; interface 693 'Lorg/hamcrest/SelfDescribing;'
03-25 15:07:01.726: W/dalvikvm(703): Link of class 'Lorg/junit/internal/AssumptionViolatedException;' failed
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): Cannot load class. Make sure it is in your apk. Class name: 'org.junit.internal.AssumptionViolatedException'. Message: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): java.lang.ClassNotFoundException: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at java.lang.Class.classForName(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at java.lang.Class.forName(Class.java:234)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfoSource.createPackageInfo(ClassPathPackageInfoSource.java:89)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfoSource.access$000(ClassPathPackageInfoSource.java:40)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:51)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfoSource$1.load(ClassPathPackageInfoSource.java:48)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.SimpleCache.get(SimpleCache.java:31)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfoSource.getPackageInfo(ClassPathPackageInfoSource.java:73)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfo.getSubpackages(ClassPathPackageInfo.java:48)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfo.addTopLevelClassesTo(ClassPathPackageInfo.java:61)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.ClassPathPackageInfo.getTopLevelClassesRecursive(ClassPathPackageInfo.java:55)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.suitebuilder.TestGrouping.testCaseClassesInPackage(TestGrouping.java:156)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.suitebuilder.TestGrouping.addPackagesRecursive(TestGrouping.java:117)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.suitebuilder.TestSuiteBuilder.includePackages(TestSuiteBuilder.java:102)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:356)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3550)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.app.ActivityThread.access$2200(ActivityThread.java:123)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.os.Looper.loop(Looper.java:126)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at java.lang.reflect.Method.invoke(Method.java:491)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  at dalvik.system.NativeStart.main(Native Method)
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703): Caused by: java.lang.NoClassDefFoundError: org.junit.internal.AssumptionViolatedException
03-25 15:07:01.746: W/ClassPathPackageInfoSource(703):  ... 26 more

这表明 hamcrst-core jar 不在类路径上,但是我已将其正确添加到测试项目的 libs 目录中:

https://github.com/junit-team/junit/blob/r4.11/src/main/java/org/junit/internal/AsductionViolatedException.java https://github.com/junit-team/junit/blob/r4.11/src/main/java/org/junit/internal/AssumptionViolatedException.java

请注意,对于 Mockito 1.9.5,我正在使用正确的(根据我的理解)Junit-4.11 的 jar。https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency


您必须确保 Mockito 的 JAR 是 APK 依赖项的一部分。

如果您使用 Intellij,请打开项目设置和模块 -> 依赖项,然后添加 Mockito 作为依赖项的一部分。

如果您使用混淆,您还必须确保它不会混淆您构建的 apk 中的mockito 类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 模拟器上的 Mockito 的相关文章

随机推荐

  • 使用 Cdata 内的 XSLT 1.0 删除 xml 声明 ()

    我从 SharePoint 应用程序收到这样的响应 Input
  • 在不知道其类型的情况下获取字典键值对

    我有一个对象instance为此 instance GetType GetGenericTypeDefinition typeof Dictionary lt gt 是真的 我的问题是 如何在实际不知道其泛型类型的情况下从该对象中提取键值对
  • javascript 中用于日期验证的正则表达式

    请有人提供日期验证正则表达式 这将允许以下规则是 它应该允许月 日 年 月 日 年 月 日 年 月 日 年 不允许yy 月份 30 和 31 验证的天数 闰年和非闰年的二月验证 不要尝试解析日期entirely使用正则表达式 跟随KISS原
  • 添加 firebase crashlytics 后,React 本机应用程序卡在启动屏幕上?

    我正在开发 React Native 应用程序 该应用程序运行良好 但是 当我导入 firebase crashlytics 时 如果我再次删除它 它会卡在启动屏幕上 应用程序工作正常吗 由于我是新手 我无法找到此问题的根本原因 我正在尝试
  • 使用 jquery UI 确认表单提交

    我正在尝试确认使用 ruby on Rails 创建的提交表单 但在提交之前 我有一个条件 即打开一个确认弹出窗口 询问用户是否真的想要这样做 这与默认的确认浏览器框一起使用 但现在我尝试使用 Jquery UI 来做到这一点 但它不起作用
  • 如何在 BigQuery 中构建“星级”报告(或迷你图或颜色渐变)

    假设我有以下示例输入 WITH Ratings AS SELECT A name 2 score UNION ALL SELECT B name 0 score UNION ALL SELECT C name 5 score UNION A
  • Firebase 控制台中不存在下载 URL

    firebase 存储的文件位置中不存在 url 相反 它提供了一个存储路径 请帮忙 在此输入图像描述 https i stack imgur com JFWAJ png 只需点击图片即可看到您的网址 要通过 Flutter 获取 URL
  • 如何可移植地乘以 64 位操作数并获得 128 位结果?

    对于 x64 我可以使用这个 uint64 t hi lo hi lo 64bit x 64bit multiply of c 0 and b 0 asm mulq 3 n t d hi a lo a c 0 rm b 0 cc a 0 h
  • 将 Archetype 新闻项目迁移到 Dexterity 内容类型

    我正在尝试将新闻项目中的内容复制到我编写的其他内容类型 在我的脚本中我有news项目和project物品 第二 project 是使用 Dexterity 定义的内容类型 如果我可以复制图像和正文 那就太好了news to project在
  • 如何使用端口 1025-5000 作为临时端口来解决 WinXP 问题?

    如果您使用端口 0 而不是非零端口创建 TCP 客户端套接字 则操作系统会为您选择任何空闲的临时端口 大多数操作系统选择临时端口IANA 动态端口范围 http www iana org assignments port numbers49
  • 为什么这个游戏无法开始,它正在循环[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我已经检查了这段代码的每一寸 我无法找出是什么导致它进入循环 一旦我按下按键开始 它就会播放开始噪音 然后它会转到结束屏幕 并说按按键再次
  • 调用 GC.Collect 和 GC.WaitForPendingFinalizers 时会发生死锁吗?

    鉴于以下情况 GC Collect GC MaxGeneration GC WaitForPendingFinalizers GC Collect GC MaxGeneration 考虑到多线程和垃圾收集模式 什么情况下会出现死锁WaitF
  • (1U << X) 有什么作用?

    我找到了这段代码 enum IsDynamic 1U lt lt 0 IsSharable 1U lt lt 1 IsStrong 1U lt lt 2 什么是 1U lt lt X do 它设置位掩码 1U lt lt 0 1 1U lt
  • 如何在mongoengine中通过oid搜索文档

    我需要通过 oid 从 db 获取文档 例如 Docs objects id 4f4381f4e779897a2c000009 但如何做到这一点 如果 id 需要 ObjectId 对象 即使我尝试从 pymongo 设置 ObjectId
  • 时间计算类型不匹配

    我正在尝试计算seconds两个日期之间的差异保留两位小数 但是 每当我执行代码时 都会收到错误 13 类型不匹配 我在网上搜索并尝试了许多变体 但总是以类型不匹配而告终 我不知道如何解决这个问题 有人可以教我怎么做吗 INFO 最初使用的
  • 使用“make-series”运算符而不定义确切的日期范围

    我在用make series创建一个错误仪表板 以指定的时间间隔显示给定时间段内的事件 如下所示 make series dcount id default 0 on timestamp from ago 30d to now step 8
  • 使用 pyinstaller 在所有系统上保留字体

    我使用 tkinter 制作了一个 GUI 我用 pyinstaller 创建了 onefile exe 但设置的字体 font freesans ttf 不适用于其他计算机 我想我需要添加字体 但在与我类似的情况下 我不明白 pygame
  • 球体表面上测地线(最短距离路径)之间的交点

    我进行了广泛的搜索 但尚未找到该问题的合适答案 给定球体上的两条线 每条线由起点和终点定义 确定它们是否相交以及相交的位置 我找到了这个网站 http mathforum org library drmath view 62205 html
  • PowerShell 未启动

    由于某种原因 PowerShell 不再在我的计算机上打开 打开 PowerShell 会显示以下内容 但永远不会完成打开并且永远不会显示提示 Windows PowerShell Copyright C Microsoft Corpora
  • Android 模拟器上的 Mockito

    Android 新手尝试在 Android 中使用我最喜欢的 Java 测试工具 我正在尝试使用 Mockito 1 9 5 如以下博客文章中所述 但无法在我的模拟器上运行测试 我目前没有用于测试的物理设备 Mockit Android 教