PlainObjectAssertion
Table of Contents
Javadoc: Javadoc.io > Junisert API > PlainObjectAssertion
Is used to assert on a plain object just carrying properties, like POJO, DTO, Value Object, you name it.
hasGetters
Asserts that the unit has a working getter for all instance fields. This assertion is flexible and accepts both Java Bean compliant and builder/record styles and does not enforce public visibility.
Example valid getters, assuming the field type is a boolean:
// Java Bean style
public boolean getField() {
return field;
}
// Java Bean style
public boolean isField() {
return field;
}
// Record style
public boolean field() {
return field;
}
Test:
@Test
void getters() {
Junisert.assertThatPojo(PlainObject.class).hasGetters();
}
hasSetters
Asserts that unit has a working setter for all instance fields. This assertion is flexible and accepts both Java Bean compliant and builder/record styles and does not enforce public visibility.
Example valid setters, assuming the field type is a boolean and class is named PlainObject:
// Java Bean style
public void setField(boolean field) {
this.field = field;
}
// Mix of Java Bean and builder style
public PlainObject setField(boolean field) {
this.field = field;
return this;
}
// Record style
public void field(boolean field) {
this.field = field;
}
// Builder style
public PlainObject field(boolean field) {
this.field = field;
return this;
}
// Builder style with-prefix
public PlainObject withField(boolean field) {
this.field = field;
return this;
}
Test:
@Test
void setters() {
Junisert.assertThatPojo(PlainObject.class).hasSetters();
}
implementsEqualsAndHashCode
Asserts that unit implements both equals and hashCode and they work as intended. These are asserted together since it’s generally necessary to maintain object contract, that units overriding one should also override the other. If equals pass assertion, so should hashCode also since they harmonize.
Test:
@Test
void equalsAndHashCode() {
Junisert.assertThatPojo(PlainObject.class).implementsEqualsAndHashCode();
}
implementsToString
Asserts that unit implements toString, so that it returns a suitable textual representation of the object. This assertion will enforce that toString contains the name of the unit and all instance fields with their current value is shown. For field check this asserts that <property name>, operator = or : and <property value> is present together for every instance field.
Standard typical toString:
@Override
public String toString() {
return "PlainObject{" +
"booleanField=" + booleanField +
", stringField='" + stringField + '\'' +
", arrayField=" + Arrays.toString(arrayField) +
'}';
}
Other accepted variant with colon:
@Override
public String toString() {
return "PlainObject{" +
"booleanField:" + booleanField +
", stringField:'" + stringField + '\'' +
", arrayField:" + Arrays.toString(arrayField) +
'}';
}
Or like JSON:
@Override
public String toString() {
return "PlainObject{" +
"\n \"booleanField\": " + booleanField +
",\n \"stringField\": \"" + stringField + '\"' +
",\n \"arrayField\": " + Arrays.toString(arrayField) +
"\n}";
}
Test:
@Test
void toStringMethod() {
Junisert.assertThatPojo(PlainObject.class).implementsToString();
}
isWellImplemented
This is a convenience asserting operation that performs all assertions in recommended sequential order. This operation performs following assertions, skipping hasSetters for immutable units: