amique
amique

Reputation: 2226

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.

Upvotes: 1

Views: 2648

Answers (2)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

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

Upvotes: 3

Mureinik
Mureinik

Reputation: 311308

In order to mock a void method you simply call it on the mock object before calling replay.

@Test
public void testSomething() {
    Custom mock = createMock(Custom.class);
    mock.process(any(MyObject.class);
    replay(mock);

    // Your test comes here

    // Optional - check the process was called
    verify(mock);
}

Upvotes: 3

Related Questions