PowerMock + EasyMock:无需调用的私有无效方法

编程入门 行业动态 更新时间:2024-10-26 22:21:45
本文介绍了PowerMock + EasyMock:无需调用的私有无效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好时间!

我需要用模拟实现替换类的私有void方法,并且无法弄清楚如何做到这一点.我尝试使用这样的构造:

I need to substitute the class' private void method with a mock implementation, and can't to figure out how to do this. I've tried to use such a construction:

Test test = PowerMock.createPartialMock(Test.class, "setId"); PowerMock.expectPrivate(test , "setId", EasyMock.anyLong()).andAnswer( new IAnswer<Void>() { @Override public Void answer() throws Throwable { return null; } }); PowerMock.replay(test);

但是内部PowerMock的类WhiteBox调用了我的"setId"方法,这对我的任务来说是错误的.有人可以建议如何避免方法调用,并可能用自定义方法替换方法主体吗?

but the internal PowerMock's class called WhiteBox invokes my "setId" method which is wrong for my task. Could someone, please, suggest, how to avoid the method invokation and possibly to replace the method body with a custom one?

推荐答案

最后.我有解决方案. 问题是我错过了以下注释:

Finally. I've got the solution. The problem was I missed the following annotations:

@RunWith(PowerMockRunner.class) @PrepareForTest(Test.class)

无论如何,要使PowerMock正常工作,我似乎需要添加一些注释,这令人感到困惑.如果那不是遗留代码,我会更喜欢Mockito.

Anyway, it seems rather confusing that to make the PowerMock working I need to add some annotations. If that wasn't a legacy code I'd prefer Mockito.

更多推荐

PowerMock + EasyMock:无需调用的私有无效方法

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

发布评论

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

>www.elefans.com

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