EasyMock: Mocked object is calling actual method

泄露秘密 提交于 2019-12-19 05:52:31

问题


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);

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

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

Thanks, AndyS


回答1:


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.



来源:https://stackoverflow.com/questions/7210171/easymock-mocked-object-is-calling-actual-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!