Compilation error when using EasyMock.expect () in a very simple example?

I try a very simple example using EasyMock , but I just can't build it. 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(... :

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

Can someone point me in the right direction? I am completely lost.

+8
java unit-testing compiler-errors easymock
source share
4 answers

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

Here is 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; } }); } 
+10
source share

Since your send () method returns void, just call the layout method with the expected values ​​and reproduce:

 SomeInterface mock = EasyMock.createMock(SomeInterface.class); SomeBase expected = new DerivesFromSomeBase(); mock.send(expected); replay(mock); 
+8
source share

Since you are mocking the interface, the only purpose of mocking the method would be to return the result of this method. In this case, it seems your send method return type is invalid. The EasyMock.expect method is generic and expects a return type, which causes the compiler to tell you that you cannot use the void method because it does not have a return type.

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

0
source share

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

0
source share

All Articles