在 Android 中模拟静态函数

编程入门 行业动态 更新时间:2024-10-10 12:20:29
本文介绍了在 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 中模拟静态函数

本文发布于:2023-10-31 06:43:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   函数   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!