在Android上使用JMock
如何在Android上使用JMock?我有几篇文章说这是不可能的,但肯定有某种方法可以做到这一点吗?
问题似乎是让 Android 识别 JMock jar 文件。那么也许有一个解决方案可以将 jar 放入资产中并制作自定义类加载器?这看起来很麻烦,但是听起来可行吗?
How can I use JMock on the Android? I've several posts saying its not possible, but surely there's some way to do it?
The issue seems to be getting the Android to even recognize the JMock jar file. So maybe there's a solution with putting the jar into assets and making a custom class loader? That seems like a lot of trouble, but does it sound like it would work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有 JMock 在 Android 开源测试项目中工作。
是的:JMock 代码在您手中的设备上执行;-)
我使用过:
请参阅以下 Android JMock 测试用例 http://github.com/olibye/ToneDial/blob/master/ToneDialTest/src/net/xpdeveloper/dialer/test/TestDialServiceTest.java
这是我遇到的三个问题克服:
Hamcrest 打包
Hamcrest 的多重打包未能通过 Android 包验证。
您在 Android ADK 中遇到的错误是:-
生成最终存档时出错:重复条目:LICENSE.txt
解决方案
unzip hamcrest-core-1.1.jar
将 hamcrest-library-1.1.jar 解压到同一目录中。
第二次解压缩会覆盖 MANIFEST.MF 和 LICENCE.TXT。
zip ../hamcrest-all-1.1.jar
最初我向 Steve 和 Nat 向 JMock
但这实际上是 Joe 在 Hamcrest 中的打包问题(所以我只是将其发布在那里;-)
JUnit 打包
eclipse 中的 JUnit 插件包含 Hamcrest 的子集。
解决方案
您需要在 eclipse 项目属性中将 JUnit 库移到 JMock 之后的类路径中。
Android 打包
JUnit v3 是 android.jar 的一部分,因此您不能使用 JUnit 4 样式的 JMock 测试。
我们还没有注释;-)
I have JMock working inside Android opensource test projects.
Yes: JMock code executes on the device in your hand ;-)
I've used:
See the following for a Android JMock Test case http://github.com/olibye/ToneDial/blob/master/ToneDialTest/src/net/xpdeveloper/dialer/test/TestDialServiceTest.java
Here are the three issues I had to overcome:
Hamcrest Packaging
The multiple packaging of Hamcrest fails Android package validation.
The error you get in Android ADK is:-
Error generating final archive: duplicate entry: LICENSE.txt
Solution
unzip hamcrest-core-1.1.jar
unzip hamcrest-library-1.1.jar in the same directory.
This the second unzip overwrites MANIFEST.MF and LICENCE.TXT.
zip ../hamcrest-all-1.1.jar
Initially I raised this with Steve and Nat for JMock
However it's really Joe's packaging issue in Hamcrest (so I just posted it there ;-)
JUnit Packaging
The JUnit plugin in eclipse contains a subset of Hamcrest.
Solution
You need to move the JUnit library down your classpath after JMock, in the eclipse project properties.
Android Packaging
JUnit v3 is part of android.jar so you can't use JUnit 4 style JMock tests.
No annotations for us yet ;-)
时代已经变了,由于事情正在修复和改进,@byeo 的答案比必要的更加复杂。
使用 Android Studio 3.1.4,我能够让 JMock 和 Hamcrest 轻松工作,至少在单元测试中是这样。我将以下依赖项添加到我的 build.gradle 文件中:
但是,由于这个问题,我不确定 JMock 是否能在使用模拟器或真实硬件的测试中正常工作:
JMock jar 在 Android 测试项目中不起作用(项目无法构建)
Times have changed and @byeo's answer is more complex than necessary due to things being fixed and improved.
With Android Studio 3.1.4, I was able to get JMock and Hamcrest working easily, at least in Unit Tests. I added the following dependencies to my build.gradle file:
However, I'm unsure if JMock will work correctly in tests that use the Emulator or real hardware because of this question:
JMock jars do not work inside Android test project (project doesn't build )