PowerMock + EasyMock:无需调用的私有无效方法 [英] PowerMock + EasyMock: private void method without invokation

查看:252
本文介绍了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:无需调用的私有无效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆