Reputation: 30128
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.
Upvotes: 8
Views: 16193
Reputation: 2176
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;
}
});
}
Upvotes: 10
Reputation: 5281
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.
Upvotes: 0
Reputation: 560
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.
Upvotes: 0
Reputation: 49915
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);
Upvotes: 9