8

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.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135

4 Answers4

10

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;
        }
    });
}
Jasper
  • 2,166
  • 4
  • 30
  • 50
  • Your example does not compile. `IAnswer` is a generic and needs a template. If I use `IAnswer` there is not method `getCurrentArguments()` available within the scope of the `answer()` method. What am I doing wrong? – Bjarke Freund-Hansen Aug 29 '11 at 08:42
  • @bjarkef you're right it should be `EasyMock.getCurrentArguments()`. I had a static import so I didn't notice it was missing. – Jasper Aug 29 '11 at 08:47
9

Since your send() method returns void, just call the mock method with expected values and replay:

SomeInterface mock = EasyMock.createMock(SomeInterface.class);
SomeBase expected = new DerivesFromSomeBase(); 
mock.send(expected);
replay(mock);
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • After `replay()` I am then expected to perform the action that would actually cause `mock.send()` to happen. And EasyMock will assert that the same calls as I expect with the same parameters are executed from my class under test? – Bjarke Freund-Hansen Aug 29 '11 at 05:23
0

Since you are mocking an interface, the only purpose in mocking a method would be to return a result from that method. In this case, it appears your 'send' method's return type is void. The 'EasyMock.expect' method is generic and expects a return type, which is causing the compiler to tell you that you can't use a void method because it doesn't have a return type.

For more information, see the EasyMock API documentation at http://easymock.org/api/easymock/3.0/index.html.

Joshua Marble
  • 540
  • 3
  • 9
0

You can't script methods with a void return in that way; check out this question for a good answer on how you can mock the behavior of your send method on your expected object.

Community
  • 1
  • 1
Mike Partridge
  • 5,128
  • 8
  • 35
  • 47