2

From the documentation, my understanding is that expectLastCall() is to be used for void methods, not expect(), accordingly I get a complaint in Eclipse when doing so.

Capture<String> capturedArgument = new Capture<>();
expect(testObject.voidMethodName(capture(capturedArgument)));

This How to expect void method call with any argument using EasyMock doesn't work.

user1821961
  • 588
  • 1
  • 10
  • 18

2 Answers2

1
TestClass testObj = new TestClass();

Capture<String> capturedArgument = new Capture<>(); //change type as needed
testObj.voidMethodName(capture(capturedArgument));
expectLastCall().atLeastOnce();//adjust number of times as needed
//may need additional replay if you have an additional mocks control object
replay(testObj);

testObj.methodUnderTest();
user1821961
  • 588
  • 1
  • 10
  • 18
1

You just need to call the void method to record it.

Capture<String> capturedArgument = new Capture<>();
testObject.voidMethodName(capture(capturedArgument));
replay(testObject);

That's it. This implicitly means you expect the void method to be called once. Adding expectLastCall().once(); or expectLastCall(); means the exact same thing and is useless.

You can't call expect() since a void method doesn't return anything. So you can't pass it in parameter to expect().

You might wonder why expectLastCall() exists then. For two reasons:

  1. For special "returns" like expectLastCall().andThrow(e)
  2. To record a specific number of calls like expectLastCall().atLeastOnce()
Henri
  • 5,551
  • 1
  • 22
  • 29