Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock: Mocked object is calling actual method

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

like image 820
AndyT Avatar asked Aug 26 '11 20:08

AndyT


People also ask

What method is used to make EasyMock?

The expect() method tells EasyMock to simulate a method with certain arguments. The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available.

Is EasyMock a mocking framework?

EasyMock is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. EasyMock is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.

Which among the following are benefits of using EasyMock Mcq?

Benefits of EasyMockNo Handwriting − No need to write mock objects on your own. Refactoring Safe − Renaming interface method names or reordering parameters will not break the test code as Mocks are created at runtime. Return value support − Supports return values. Exception support − Supports exceptions.


1 Answers

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.

like image 102
jhericks Avatar answered Oct 13 '22 07:10

jhericks