0

Just trying the EasyMock for the first time.

I seem to get it going but I am immediately halted with the fact that the mocked class runs a method "returning" void (EntityManager.remove(abc)).

I am able to mock the EntityManger partly to begin testing, i.e.

EasyMock.expect(this.mockManager.find(Some.class, id)).andReturn(mock);

, but how do I do the same for the 'remove' case?

I can't do (for example):

EasyMock.expect(this.mockManager.remove(rek)).andReturn(Boolean(true));

And if I do nothing, I get:

java.lang.AssertionError: 
Unexpected method call EntityManager.remove(EasyMock for class my.package.Some)...

I need to test the logic before getting to remove part, but I don't care if it actually succeeds (would be a different thing).

Ville Myrskyneva
  • 1,560
  • 3
  • 20
  • 35

1 Answers1

1

You don't need to call EasyMock.expect(). Just use

this.mockManager.remove(rek);

while in the recording phase (before calling replay()).

If you want the mocked method, for example, to throw an exception or to be called twice, use expectLastCal():

this.mockManager.remove(rek);
expectLastCall().andThrow(new RuntimeException());
//or expectLastCall().times(2);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I actually figured this just out by my self before I checked here again (though not the Exception part yet). You were fast. +1 For that. – Ville Myrskyneva Apr 30 '13 at 11:49