Android Junit 测试 - Mockito 类未找到异常
我正在尝试在 Android 上构建一个单元测试,该测试使用来自mockito.org 的mockito-all-1.8.5.jar 文件。我使用 project > 将 jar 文件添加到我的项目中属性>项目构建路径>添加外部罐子。这对我在非 Android java 项目上一直有效。
但是,当我运行单元测试时,出现以下未找到类异常:
java.lang.ClassNotFoundException: org.mockito.runners.VerboseMockitoJUnitRunner
上面是以下消息:
无法加载类。确保它在您的 apk 中。类名:“org.mockito.runners.VerboseMockitoJUnitRunner”。消息:org.mockito.runners.VerboseMockitoJUnitRunner
我检查了 jar,该类就在那里。
我还尝试根据此链接添加:
如何在我的 Android 应用程序中使用和打包 JAR 文件?
但是,仍然没有运气。我怎样才能找到这个班级?
编辑:我认为 Mockitto 依赖的任何库都存在问题 - 它们可能与 Dalvik jvm 不兼容。请参阅这篇文章:
http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/
1) 导入将模拟框架(例如mockito)作为附加依赖项添加到项目中。 任何包含未编译为 Dalvik 字节码(大多数)的类文件的导入 jar 都将不起作用。尝试将源代码与您的项目一起编译也不会起作用,因为大多数库将广泛使用与 Dalvik 不兼容的 Java 语言部分:它使用构建在 Apache Harmony Java 实现的子集上的自己的库。
I'm attempting to build a unit test on Android which utilizes the mockito-all-1.8.5.jar file from mockito.org. I added the jar file to my project using project > properties > project build path > add external jar. This has always worked for me on non-Android java projects.
However, when I run the unit test, I get the following class not found exception:
java.lang.ClassNotFoundException: org.mockito.runners.VerboseMockitoJUnitRunner
Above it is the following message:
Cannot load class. Make sure it is in your apk. Class name: 'org.mockito.runners.VerboseMockitoJUnitRunner'. Message: org.mockito.runners.VerboseMockitoJUnitRunner
I checked the jar and the class is there.
I also tried adding according to this link:
How to use and package a JAR file with my Android app?
But, still no luck. How can I get this class to be found?
Edit: I'm thinking there is a problem with whatever libraries Mockitto depends on - they may not be compatible with the Dalvik jvm. See this post:
http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/
1) Import a mocking framework (e.g. mockito) into the project as an additional dependency.
Any imported jars containing class files not compiled to Dalvik bytecode (most) will not work. Attempting to compile the source along with your project will not work either because most libraries will make extensive use of parts of the Java language not compatible with Dalvik: it uses its own library built on a subset of the Apache Harmony Java implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 Dalvik JVM 内执行,您将无法使用 Mockito。
但是,如果您在桌面 JVM 上运行单元测试(例如使用 Robolectric 等测试框架)那么你就可以走了。我目前在我的Android项目中将两者结合起来,效果很好。
If you are executing inside the Dalvik JVM you won't be able to use Mockito.
If you are running your unit tests on your desktop JVM however (with a testing framework like Robolectric for example) then you are good to go. I am combining the two on my Android projects at present, to good effect.