3

The setup, java 8 using lombok, meanbean and equalsverifier(huge fan of all 3, and also limited in what versions I can use per co. policy - on 2.3.3 of equalsverifier):

@Data
@NoArgsConstructor
class A {...}

@Data
@NoArgsConstructor
class B extends class A {...}

I get the Significant fields error calling EqualsVerifier:

EqualsVerifier.forClass(B.class)
                .withRedefinedSuperclass()
                .suppress(Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS, Warning.INHERITED_DIRECTLY_FROM_OBJECT).verify();

Changed class B as follows, and got redefined superclass error instead:

@NoArgsConstructor
@ToString(callSuper=false)
class B extends class A {...}

Changed class B again as follows, and got significant fields error instead:

@NoArgsConstructor
@ToString(callSuper=false)
@EqualsAndHashCode(callSuper=false)
class B extends class A {...}

Any suggestions? I don't know how to get passed this one.

1 Answers1

1

Creator of EqualsVerifier here.

I don't quite have enough information to be able to help you -- what fields doe A and B have? Also, I'm not sure if Lombok's @Data can be inherited; you'll have to check their documentation or run some experiments.

That said, combining equals with inheritance correctly is hard to get right. The advice I usually give people is, don't do it. If you have to, I've written something about it in the EqualsVerifier manual. I'd also recommend reading this article for some background info.

As general rules of thumb:

  • Always call super in your equals methods if you override them and intend to add state.
  • Make your classes final if you don't need to extend from them. In this case, B could be final, which would make it easier for you to appease EqualsVerifier.
  • If you don't know what code Lombok generates, use Delombok.
jqno
  • 15,133
  • 7
  • 57
  • 84
  • I have a field named `createdDate` and is not a part of equals method, is there anyway I can use `EqualsVerifier` in this case, I am getting similar error. – Nisarg Patil Dec 17 '20 at 14:01
  • Sure: use `withIgnoredFields`. See https://jqno.nl/equalsverifier/manual/ignoring-fields/ for more info. – jqno Dec 17 '20 at 18:51