且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Android 项目中使用 PowerMock?

更新时间:2023-10-06 14:04:10

抱歉,你不能在 Dalvik VM 中使用 PowerMock.

Sorry, you can't use PowerMock in the Dalvik VM.

PowerMock 通过在使用 的自定义 ClassLoader 下运行您的测试来工作Javassist 来修改你的类的字节码.这在普通 JVM 上可以正常工作,但在 Dalvik 上,字节码和类格式不同,所以这种方法不起作用.需要重写 PowerMock 以使用 Dexmaker 而不是 Javassist - 这绝对不是微不足道的,并且我在 PowerMock 问题列表上没有看到类似的内容.

PowerMock works by running your test under a custom ClassLoader which uses Javassist to modify the bytecode of your classes. This works okay on a normal JVM, but on Dalvik the bytecode and class format are different, so this approach doesn't work. PowerMock would need to be rewritten to use Dexmaker instead of Javassist - this would be decidedly non-trivial, and I don't see anything like this on the PowerMock issues list.