有什么方法可以使用任何模拟框架在 Android 中模拟静态函数.Mockito 可以模拟类,但不足以模拟静态函数.
Is there any way I can Mock Static Function in Android using any Mocking Framework. Mockito can mock classes but is insuffiecient to mock Static functions.
任何帮助将不胜感激.
提前致谢
推荐答案Mocking 使用面向对象、继承等概念....
Mocking works by using the concepts of Object Orientation, Inheritance etc....
基本上是通过覆盖某些方法&看起来像真实对象的对象/实例中的行为,因为它们是这些真实对象的子类.
Basically by overriding certain methods & behaviour in objects / instances that look like real objects, because they are subclasses of these real objects.
换句话说,mocking 部分来自实例上的覆盖方法.
In other words, the mocking part comes in overriding methods on instances.
无法覆盖静态方法 (afaik).
It is not possible to override a static method (afaik).
因此模拟静态调用并不容易(如果可能的话).
Therefore mocking of static calls is not easy (if even possible).
事实证明,我在上述声明中错误地认为这是不可能的.
As it turns out, I was wrong in my above statement that it is not possible.
我应该在这个网站上搜索重复的问题.请参阅下面的一些框架链接,这些框架声称在某些情况下可以为您执行此操作.由于它们使用字节码,我不确定它们是否能在 Android (ymmv) 上正常工作.
I should have searched this site for duplicate questions. See below for some links to frameworks that claim to do this for you in some cases. Since they work with bytecode, I'm not sure they will work properly on Android (ymmv).
模拟静态方法
如何才能我很容易在 Java (jUnit4) 中模拟出一个静态方法
(感谢 Rohit 强迫我重新评估我的信仰)
(thanks to Rohit for forcing me to reassess my beliefs)
更多推荐
在 Android 中模拟静态函数
发布评论