Menu

#15 Can't expect equals() on two mocks

EasyMock_2.4
open
5
2014-08-06
2008-11-27
James Shaw
No

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)

Discussion

  • James Shaw

    James Shaw - 2008-11-27

    Test case

     
  • Henri Tremblay

    Henri Tremblay - 2008-11-28

    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?

     
  • James Shaw

    James Shaw - 2008-11-29

    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;
    }
    }
    }

     
  • SourceForge Robot

    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).

     
  • James Shaw

    James Shaw - 2008-12-13

    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?

     
  • Henri Tremblay

    Henri Tremblay - 2008-12-13

    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.

     
  • Henri Tremblay

    Henri Tremblay - 2009-09-12

    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.

     
  • SourceForge Robot

    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).

     
  • Henri Tremblay

    Henri Tremblay - 2009-09-27

    Oh dear, I'm repeating myself... I've reopened it and I'll see what I can do.

     
  • Virtualwarp

    Virtualwarp - 2011-03-01

    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.

     

Log in to post a comment.