Stumbled across this easy way to test domains, its called MeanBean
Typically many projects don’t bother with testing domain classes. They hope that other tests in the suit will do that. Perhaps, but I have found many bugs with hashcode and equals over the years. It’s very dull testing these beans there has to be a better way!
Meanbean from Graham Williamson makes it possible to do it in one class.
Given these two simple domain classes
public class PersonId {
private long id;
public PersonId() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonId personId = (PersonId) o;
if (id != personId.id) return false;
return true;
}
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("id", id).toString();
}
}
public class Person {
private PersonId personId;
private String firstName;
private String surName;
private String middleName;
public Person() {
}
public PersonId getPersonId() {
return personId;
}
public void setPersonId(PersonId personId) {
this.personId = personId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return false;
if (personId != null ? !personId.equals(person.personId) : person.personId != null) return false;
if (middleName != null ? !middleName.equals(person.middleName) : person.middleName != null) return false;
if (surName != null ? !surName.equals(person.surName) : person.surName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = personId != null ? personId.hashCode() : 0;
result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
result = 31 * result + (surName != null ? surName.hashCode() : 0);
result = 31 * result + (middleName != null ? middleName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("personId", personId)
.add("firstName", firstName)
.add("surName", surName)
.add("middleName", middleName)
.toString();
}
}
This is the meanbean domain test. This tests all set/get methods and the hashcode and equals
public class testDomain {
@Test
public void testDomain () {
Configuration configuration = new ConfigurationBuilder()
.iterations(10).build();
final BeanTester beanTester = new BeanTester();
EqualsMethodTester equalsTester = new EqualsMethodTester();
HashCodeMethodTester hashTester = new HashCodeMethodTester();
beanTester.testBean(PersonId.class);
equalsTester.testEqualsMethod(PersonId.class);
hashTester.testHashCodeMethod(PersonId.class);
beanTester.testBean(Person.class);
equalsTester.testEqualsMethod(Person.class);
hashTester.testHashCodeMethod(Person.class);
}
}
How easy is that? No excuse not too test that domain now!












