在 Android 中模拟静态函数 [英] Mocking Static Functions in Android

查看:36
本文介绍了在 Android 中模拟静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用任何模拟框架在 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 中模拟静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆