Can't expect equals() on two mocks
Brought to you by:
tammofreese
The attached test case gives the error:
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:174)
at org.easymock.EasyMock.expect(EasyMock.java:156)
at MyTest.test(MyTest.java:17)
Test case
equals, toString and hashCode can't be mocked. This is by design to be compliant with EasyMock behavior for interfaces.
Why do you need it?
Because I have a bean with a fairly large equals() method that is called by the class under test. Instead of populating two beans, which would need many more lines of code, I was hoping that I could just mock them up with classextensions.
As a workaround, I've extended them anonymously and overridden the equals method as needed, e.g.
final Bean bean2 = new Bean();
Bean bean1 = new Bean() {
public boolean equals(Object o) {
if (o == bean2) {
return true;
} else {
fail('Bad comparison');
return false;
}
}
}
This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Closed automatically after 14 days? Before this bug disappears, could someone confirm whether or not this feature is a) possible to implement and b) desirable to have?
No, sorry, I just forgot to turn it back to open after your answer. I'll try to keep the default behavior but to allow overloading with a recording.
However, the drawback is that if EasyMock class toString / equals / hashcode, it's the recording that will be used. So that can be quite strange from the user point of view... So maybe your overloading solution is still the simpler.
equals is internally used by EasyMock, it can't be mocked. However, the internal implementation is to do == on them. That could be a workaround.
This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Oh dear, I'm repeating myself... I've reopened it and I'll see what I can do.
I found this bug report while searching and have a idea how this could be solved.
I don't know if EasyMock reuses the InvocationHandler instances across multiple mock (proxy) instances, but if so it could use equals and hashcode from the InvocationHandler insteadof the methods on the proxy interally. By default equals and hashcode could be redirected to the invocationHandlers equals and hashcode. As soon as someone defines an expectation for equals/hashcode or calls a special method to configure the mock, the automatic behavior is disabled.