PowerMock可以不加注解地使用吗?
我正在尝试在另一个应用程序中使用 PowerMock 作为库。有没有办法使用它来模拟静态方法调用而不使用注释(我在 Clojure 中,它并没有真正执行注释)
I'm trying to use PowerMock as a library in another application. Is there a way to use it to mock a static method call without using annotations (I'm in Clojure which doesn't really do annotations)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 powermock 支持和 这个博客,我猜测测试中没有办法避免注释。我想我们需要
@PrepareForTest(StaticClass.class)
但是。所以我相信至少不可能避免@PrepareForTest
。也许我错了,但只是想分享我发现的东西。According to powermock support and this blog, I guess there is no way to avoid annotations in test. I guess we need the
@PrepareForTest(StaticClass.class)
however. So I believe it is not possible to avoid@PrepareForTest
atleast. May be I am wrong, but just thought of sharing what I found.事实上,这是可能的,尽管最终解决的过程是痛苦的。
PowerMock 运行器只是在不同的类加载器中初始化测试环境,其中
PrepareForTest
注释中指定的类由 Javassist 进行调整。所以假设你模仿类加载器的工作并自己调用Javassist,你可以达到相同的效果。作为一个例子,我利用 PowerMock(没有注释的内部)来发现给定方法引用的方法名称。更多信息可以在我的博客上找到(捷克语,带有工作示例)。我强调这样的用法只是实验性的,不适合生产使用。
In fact, it's possible, although the way to final solution is painful.
PowerMock runner just initializes test environment in different classloader, in which the classes specified in
PrepareForTest
annotation are tweaked by Javassist. So assumed you mimic the work of the classloader and call Javassist by yourself, you can achieve the same effect.As an example, I utilized PowerMock (internals without annotations) to discover name of method for given method reference. Further info can be found on my blog (in Czech, with working examples). I emphasize such an usage is only experimental and not suitable for production usage.