是否可以模拟 Java 协议缓冲区消息?
Protocol buffer 类被标记为 final
,大概是为了提高效率;然而,这使得它们很难测试—— Mockito 无法模拟/监视最终类。我尝试使用 PowerMockito
但没有成功:在准备用于测试的 final
类时,我收到 ClassFormatError
。
到目前为止,我的解决方案是创建可模拟的适配器接口,但我希望有一种不那么费力的方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JMockit 可以处理
final
和static
。只需注意如何设置它,因为它需要 -javaagent JVM 参数,或类路径调整,或额外的注释才能模拟final
和static
内容。JMockit can handle
final
andstatic
. Just pay attention to how to set it up as it requires the -javaagent JVM parameter, or classpath tweaks, or extra annotations to be able to mockfinal
andstatic
stuffs.JDave 有一个与 Unfinalizer .jmock.org/mocking-classes.html" rel="nofollow noreferrer">JMock ClassImposteriser
它无法取消从启动类加载器加载的类,并且在启动测试时需要一个 VM 参数。
JDave has an Unfinalizer that integrates with the JMock ClassImposteriser
It can't unfinalize classes loaded from the the boot classloader, and it requires a VM argument when launching the tests.