0

Found a workaround. See solution at the bottom

Ok I have been in EasyMock jail all day and I need some help getting out. I have a void save() method I am mocking out.

public void save(PurchaseOrder po);

PurchaseOrder is an abstract class that has two children

USPurchaseOrder
CAPurchaseOrder

Here's my code in the JUnit test

MyDAO myDAO = createMock(MyDAO.class);
PurchaseOrder usPurchaseOrder = new USPurchaseOrder(msgUS);
myDAO.save(usPurchaseOrder);
expectLastCall().atLeastOnce();

PurchaseOrder caPurchaseOrder = new CAPurchaseOrder(msgCA);
myDAO.save(caPurchaseOrder);
expectLastCall().atLeastOnce();

replay(myDAO);

//execute code that uses DAO

I get the following error: Unexpected method call MyDAO.save(USPurchaseOrder@1a70b8):

The only problem here is that the DAO signature does not require a USPurchaseOrder, only a PurchaseOrder, which is what I am passing in.

Even doing this produces the same error

myDAO.save(new USPurchaseOrder(msgUS));

What am I doing wrong?

Workaround Ok, I kept plugging away at this and while I don't understand why I am getting the error, I added the anyObject() to the code to get it to work.

MyDAO myDAO = createMock(MyDAO.class);

myDAO.save(anyObject(OrderRequest.class));

myDAO.save(anyObject(OrderRequest.class));

replay(myDAO);

//execute code that uses DAO
Nathan Weddle
  • 187
  • 3
  • 11

1 Answers1

0

For matching method calls EasyMock uses equals() for method parameters. You should take it into account. So my guess is that you have not implemented equals() in your USPurchaseOrder and the default Object.equals() behavior is used, that compares whether the instances of USPurchaseOrder are the same, which are apparently not.

Vic
  • 1,778
  • 3
  • 19
  • 37