# Abstracting getter and setter method references for testing

Is the following code easy to understand? If not how should it be changed to be more understandable. The purpose of this is reducing similar code in unit tests. This avoids repeating lines for calling getters, setters, and checking for equality.

/**
* Example string for testing.
*/
private static final String STRING_EXAMPLE="stringExample";

/**
* Test that a getter and setter are consistent.
*/
@Test
public void testGetterSettersForA() {
A a = new A();
assertGetterSetterConsistent(STRING_EXAMPLE,a::setC,a::getC);
}

/**
* Assert that a getter and setter are consistent.
* @param input the object to be set.
* @param setter the setter.
* @param getter the getter.
*/
private static <T> void assertGetterSetterConsistent(T input, Consumer<T> setter, Supplier<T> getter) {
boolean consistent =isSetConsistentToGet(input,setter,getter);
assertTrue("Getter and Setter should be consistent.",consistent);
}

/**
* Check if the object being set is the same as the object being gotten.
* @param input the object to be set.
* @param setter the setter.
* @param getter the getter.
* @return whether the object being set is the same as the object being gotten.
*/
private static <T> boolean isSetConsistentToGet(T input, Consumer<T> setter, Supplier<T> getter) {
setter.accept(input);
T getValue = getter.get();
return areEqual(input,getValue);
}

/**
* Null safe check if two objects are equal.
* @param first the first object.
* @param second the second object.
* @return whether the two objects are equal.
*/
private static <T> boolean areEqual(Object first, Object second) {
if(first==null) {
return second==null;
}
return first.equals(second);
}

/**
* Class for testing a getter and setter.
*/
private static final class A{
/**
* Variable named c.
*/
private String c;

/**
* Get c.
* @return c.
*/
public String getC() {
return c;
}

/**
* Set c.
* @param c the value to be set.
*/
public void setC(String c) {
this.c = c;
}
}

• Welcome to codereview.se - In my view it would be better to implement equals (and hashcode) in your DTOs rather than using "external" code for that. Almost every IDE generates them for you (as a starting point) so its almost no effort. – Timothy Truckle Apr 8 at 6:48
• I believe you should only compare object reference equality (e.g. assertSame) when verifying that the value returned by getter is the same as what was passed to the setter. – TorbenPutkonen Apr 8 at 11:46

I had no issues, but I'm not sure if "easy to understand" is actually answerable.

There's still room for improvement, though: Your areEqual method can be replaced by Objects.equals.

private static <T> boolean isSetConsistentToGet(T input, Consumer<T> setter, Supplier<T> getter) {
setter.accept(input);
T getValue = getter.get();
return Objects.equals(input, getValue);
}


Let me also throw in two frameworks that might make your life easier:

1. Project Lombok. Lombok can generate getters and setters for your fields, (IMHO) removing the necessity for writing any explicit tests completely.
2. OpenPojo. I haven't used that one yet, but it claims to do getter/setter validation automagically and has been suggested on Stack Overflow.