AbstractMethodError

100 views
Skip to first unread message

Julie Burnard

unread,
May 4, 2012, 12:57:04 PM5/4/12
to equalsverifier
The Error message section clearly states that an AbstractMethodError
would occur if either equals or hashCode methods called an abstract
method:

This error can occur when the class under test, one of its
fields, or its superclass, is abstract,
and their equals or hashCode method calls an abstract method.

However in the cases that I'm testing I don't have any abstract
methods for equals or hashCode, but I do for the toString method. In
this case EqualsVerifier also throws an AbstractMethodError:

java.lang.AbstractMethodError: com/it/entity/ro/
BaseRO.createPkMap()Ljava/util/LinkedHashMap;
at com.it.entity.bo.BaseBO.formatPk(BaseBO.java:112)
at com.it.entity.bo.BaseBO.toString(BaseBO.java:137)
at java.lang.String.valueOf(String.java:1505)
at java.lang.StringBuilder.append(StringBuilder.java:194)
at
nl.jqno.equalsverifier.HierarchyChecker.checkSuperclass(HierarchyChecker.java:
85)
at
nl.jqno.equalsverifier.HierarchyChecker.check(HierarchyChecker.java:
61)
at
nl.jqno.equalsverifier.EqualsVerifier.verifyWithExamples(EqualsVerifier.java:
396)
at
nl.jqno.equalsverifier.EqualsVerifier.performVerification(EqualsVerifier.java:
366)
at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:
333)
at
com.jcoe.jab.domain.booking.lookup.CityBoTest.testEqualsHashCodeForClass(CityBoTest.java:
158)

Here is my class hierarchy;
abstract BaseBO (toString() overridden here calling an abstract
method)
abstract BaseAuditBO
abstract JabBaseAuditBO
CityBO

/**
* Returns a formatted string based on the {@link #formatPk()}
method
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

// formatPk calls an abstract method createPkMap
return this.getClass().getSimpleName() + ": " +
this.formatPk();
}

We've done this as a convienence so all our concrete "BO" objects
don't have to override toString().

Is there a way around this error using equalsVerifier?

Jan Ouwens

unread,
May 6, 2012, 11:44:19 AM5/6/12
to equalsv...@googlegroups.com
Hi Julie,

I'm afraid there's no way around this issue at the moment, apart from
making your formatPk method concrete (which, I realise, is not what
you want).

The reason is that EqualsVerifier will always compare your objects
with instances of their superclass, even if they are concrete. As you
can read in Effective Java, this is very important, because this is
where symmetry and transitivity errors most easily pop up.
Unfortunately, EqualsVerifier calls these toString methods as well, to
generate error messages. There error messages are generated whether
they are needed or not, because of the way Java works. Consider a
JUnit assert statement:

CityBO city = ...
CityBO otherCity = ...
assertEquals("Cities not equal: " + city + " and " + otherCity, city,
otherCity);

As you can see, the error message is generated before the call to
assertEquals takes place.

There is a way around this, but it would require some re-architecting
of EqualsVerifier, which I unfortunately don't have time for in the
near future.


Regards,
Jan
Reply all
Reply to author
Forward
0 new messages