javajuniteasymock

easymock - mocking to use anyObject as parma of void setter method


Using EasyMock, how can I create the mock of the following class's process method? I want to create a mock which can accept any object of type MyObject.class.

public class Custom {
    public void process(MyObject obj){
        //code
    }
}

I know how to do it if the method returns something, but with a void method I am not able to get my head around it.


Solution

  • Here's how to expect a call on a void method

    Custom mock = EasyMock.createMock(Custom.class); // create the mock
    mock.process(EasyMock.anyObject(MyObject.class)); // invoke the method
    EasyMock.expectLastCall(); // register it as expected
    EasyMock.replay(mock); // set the state
    
    
    mock.process(new MyObject()); // invoke the method in the test
    
    EasyMock.verify(mock); // verify the call