Skip to content

Instantly share code, notes, and snippets.

@MrCreosote
Created February 11, 2017 01:44
Show Gist options
  • Save MrCreosote/ffdd173574014196f8775abe7fa1ac46 to your computer and use it in GitHub Desktop.
Save MrCreosote/ffdd173574014196f8775abe7fa1ac46 to your computer and use it in GitHub Desktop.
EqualsVerifier missing instruction
import org.junit.Test;
import nl.jqno.equalsverifier.EqualsVerifier;
public class EqualsVerifierTesting {
private static class Foo {
private final String s;
public Foo(String s) {
this.s = s;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((s == null) ? 0 : s.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Foo other = (Foo) obj;
if (s == null) {
if (other.s != null) {
return false;
}
} else if (!s.equals(other.s)) {
return false;
}
return true;
}
}
private static class Bar extends Foo {
private final String s2;
public Bar(String s, String s2) {
super(s);
this.s2 = s2;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((s2 == null) ? 0 : s2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false; // never exercised
}
Bar other = (Bar) obj;
if (s2 == null) {
if (other.s2 != null) {
return false;
}
} else if (!s2.equals(other.s2)) {
return false;
}
return true;
}
}
@Test
public void equalsCoverage() {
EqualsVerifier.forClass(Bar.class).usingGetClass().verify();
EqualsVerifier.forClass(Foo.class).usingGetClass().verify();
}
}
@MrCreosote
Copy link
Author

It's because the superclass check will fail unless obj is a Bar, so it's impossible for the marked line to be exercised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment