Compile error while using EasyMock.expect() in very simple example?

Bjarke Freund-Hansen picture Bjarke Freund-Hansen · Aug 26, 2011 · Viewed 14.9k times · Source

I am trying a very simple example using EasyMock, however I simply cannot make it build. I have the following test case:

@Test
public void testSomething()
{
    SomeInterface mock = EasyMock.createMock(SomeInterface.class);
    SomeBase expected = new DerivesFromSomeBase();

    EasyMock.expect(mock.send(expected));
}

However I get the following error in the EasyMock.expect(... line:

The method expect(T) in the type EasyMock is not applicable for the arguments (void)

Can somebody point me in the correct direction? I am completely lost.

Answer

Jasper picture Jasper · Aug 26, 2011

If you want to test void methods, call the method you want to test on your mock. Then call the expectLastCall() method.

Here's an example:

@Test
public void testSomething()
{
    SomeInterface mock = EasyMock.createMock(SomeInterface.class);
    SomeBase expected = new DerivesFromSomeBase();

    mock.send(expected);

    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
        public Object answer() {
            // do additional assertions here
            SomeBase arg1 = (SomeBase) EasyMock.getCurrentArguments()[0];

            // return null because of void
            return null;
        }
    });
}