EasyMock:模拟对象正在调用实际方法 [英] EasyMock: Mocked object is calling actual method

查看:258
本文介绍了EasyMock:模拟对象正在调用实际方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元测试中遵循了代码片段,

I've following code snippet in my unit test,

ClassToBeMocked mock = createMock(ClassToBeMocked.class); //I've statically imported EasyMock.*
mock.callMethod(); //This is a void method
expectLastCall();
replay(mock);

但是,当我运行测试时,实际上并未调用callMethod()而不是引起期望.我在做错什么吗?

But when I run the test, instead of seeting up the expectaion, callMethod() is actually called. Am I doing something wrong?

我是EasyMock或任何模拟框架的新手,由于此问题而被阻止.任何帮助将不胜感激.

I'm fairly new to EasyMock or any mocking framework and blocked because of this problem. Any help would be greatly appreciated.

谢谢, 安迪斯

推荐答案

如果您使用"final"方法模拟类,则会发生这种情况. EasyMock不会覆盖最终方法.如果您无法模拟界面,并且无法将方法更改为非最终方法,则可以使用 PowerMock 以及EasyMock来解决此限制.该特定用例在此处中进行了描述.

This will happen if you are mocking a class with a 'final' method. EasyMock does not override a final method. If you cannot mock an interface, and you cannot change the method to non-final, you can use PowerMock along with EasyMock to get around this limitation. That specific use case is described here.

这篇关于EasyMock:模拟对象正在调用实际方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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