是否可以创建 android.os.Bundle 的存根以在 JVM 下进行测试?
正如标题所说,我想知道,如果可能的话,如何从 Bundle 在 JVM 上运行(所以不是 Dalvik)。由于它是 android.os 包的一部分,因此可能与这种可能性有关。
我的观点是因为我想使用在 JVM 上运行的模拟框架来测试 Activity.onCreate()
方法。
我已经找到了这个链接,但他们似乎使用了剥离了自定义 android.jar,因为它相当实验性且未经证实(但即使使用他们的 jar,我也无法重现,也许我需要一些睡眠)。
如果有另一种替代方案来模拟测试,任何见解都将一如既往地受到赞赏。
谢谢。
As the title says, I want to know, if possible, how can I create a stub from Bundle to run at JVM (so not Dalvik). Since its part of android.os
package, may be that is related to that impossibility.
My point is due to the fact that I want to use a mocking framework, which run at JVM, to test the Activity.onCreate()
method.
I've already found this link but it seems they use a stripped custom android.jar since its rather experimental and unproven (yet I could not reproduce even with their jar, maybe I need some sleep).
If there is another alternative to mock testing this, any insights will as always be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经成功地在 java jvm 上为与 android.jar 类交互的类编写了单元测试。我使用 Powermock 来模拟所有 Android 类(如 Bundle)。
此链接讨论了使用不会抛出异常的 Android jar 版本java jvm 上的“stub”异常。我不需要使用不同版本的 android jar。使用 Powermock,我已经能够成功模拟出我需要的任何东西。对我来说,这种单元测试技术不是实验性的。
话虽如此,扩展活动、片段等的类的单元测试变得非常模拟繁重。我觉得将尽可能多的逻辑转移到 pojo 类中是很好的,这样可以使 Android 扩展类尽可能精简。然后您可以决定是否值得对 Android 扩展类进行隔离 junit 测试。如果你愿意的话,这当然可以做到。您还可以考虑使用 Android 提供的集成测试框架,或者Robotium 用于测试您的 Android 扩展类及其生命周期。
I've been successfully writing unit tests on the java jvm for classes that interact with android.jar classes. I use Powermock to mock out all android classes (like Bundle).
This link talks about using a version of the android jar that does not throw the "stub" exception on the java jvm. I have not needed to use a different version of the android jar. Using Powermock I've been able to successfully mock out anything I need to. For me this unit testing technique is not experimental.
Having said that - the unit tests for classes that extend activities, fragments, etc. become very much mock heavy. I feel it's good to move as much logic as you can into pojo classes, keeping your android extended classes as thin as possible. You can decide then if you feel it's worth isolation junit testing the android extended classes. It can certainly be done if you wish. You can also consider an integration testing framework like the one provided by Android or Robotium to test your android extended classes and their lifecycles.