testUtils is a simple set of utilities aimed at helping you test and improve the coverage of your code. For the time being it is only comprised of BeanLikeTester.
This class provides methods to easily and quickly test and improve the test coverage of 'bean like' objects (i.e. objects with setters and getters but that don't necessarily implement the Serializable interface and can have non default constructors).
The beanLike objects:
All the initial values, setters, getters, non default constructors, hashCode(), equals(), toString() can be automatic tested from a map of default and non default property/value.
For example to test this simple bean:
| publicclassMyBean {    privateString  property1 = "defaultValue"; // Read/write.    privatebooleanproperty2;                  // Read/write.    privatelongproperty3 = -1;             // Read only.    publicString getProperty1() {        returnproperty1;    }    publicvoidsetProperty1(String property1) {        this.property1 = property1;    }    publicbooleanisProperty2() {        returnproperty2;    }    publicvoidsetProperty2(booleanproperty2) {        this.property2 = property2;    }    publiclonggetProperty3() {        returnproperty3;    }    @Override    publicinthashCode() {        finalintprime = 31;        intresult = 1;        result = prime * result + ((property1 == null) ? 0: property1.hashCode());        result = prime * result + (property2 ? 1231: 1237);        returnresult;    }    @Override    publicbooleanequals(Object obj) {        if(this== obj)            returntrue;        if(obj == null)            returnfalse;        if(getClass() != obj.getClass())            returnfalse;        MyBean other = (MyBean) obj;        if(property1 == null) {            if(other.property1 != null)                returnfalse;        }        elseif(!property1.equals(other.property1))            returnfalse;        if(property2 != other.property2)            returnfalse;        returntrue;    }    @Override    publicString toString() {        return"MyBean [property1="+ property1 + ", property2="+ property2 + ", property3="+ property3 + "]";    } | 
you would use instances of PropertiesAndValues and BeanLikeTester to:
| // 1. Define the default values expected:PropertiesAndValues defaultValues = newPropertiesAndValues();defaultValues.put("property1", "defaultValue");defaultValues.put("property2", false);defaultValues.put("property3", -1L);// 2. Give another value for each of the properties:PropertiesAndValues otherValues = newPropertiesAndValues();otherValues.put("property1", "anotherValue");otherValues.put("property2", true);otherValues.put("property3", 0L);// 3. Create the tester:BeanLikeTester blt = newBeanLikeTester(MyBean.class);// 4a. Test the bean's methods:blt.testDefaultValues(defaultValues);blt.testMutatorsAndAccessors(defaultValues, otherValues);blt.testEqualsAndHash(defaultValues, otherValues);blt.testToString(defaultValues, otherValues);// 4b. Or test everything at once.blt.testBeanLike(defaultValues, otherValues);// 5. Check the code coverage. The method equals() may not have been totally covered.// In this case create another set of values and run testEqualsAndHash() against it:otherValues.put("property1", null);otherValues.put("property2", true);otherValues.put("property3", 0L);blt.testEqualsAndHash(defaultValues, otherValues);// If any of the tests fails or the values given in parameter are incorrect an // exception BeanLikeTesterException is thrown. | 
Now, if you want to test an object that defines non default constructors that only set properties, e.g.:
| publicfinalclassMyBeanLike {    privatefinalString       property1;                      // Can only be set by a constructor.    privateintproperty2;                      // Can be set by either a constructor or a setter.    privatebooleanproperty3;                      // Can only be set by a setter.    privatefinalInteger      property4  = Integer.MAX_VALUE; // get only.    publicMyBeanLike(String property1) {        this.property1 = property1;    }    publicMyBeanLike(String property1, intproperty2) {        this.property1 = property1;        this.property2 = property2;    }    publicString getProperty1() {        returnproperty1;    }    publicintgetProperty2() {        returnproperty2;    }    publicvoidsetProperty2(intproperty2) {        this.property2 = property2;    }    publicbooleanisProperty3() {        returnproperty3;    }    publicvoidsetProperty3(booleanproperty3) {        this.property3 = property3;    }    publicInteger getAReadOnlyProperty() {        returnproperty4;    }    @Override    publicinthashCode() {        finalintprime = 31;        intresult = 1;        result = prime * result + (property3 ? 1231: 1237);        result = prime * result + ((property1 == null) ? 0: property1.hashCode());        result = prime * result + property2;        returnresult;    }    @Override    publicbooleanequals(Object obj) {        if(this== obj) {            returntrue;        }        if(obj == null) {            returnfalse;        }        if(!(obj instanceofMyBeanLike)) {            returnfalse;        }        finalMyBeanLike other = (MyBeanLike) obj;        if(property3 != other.property3) {            returnfalse;        }        if(property1 == null) {            if(other.property1 != null) {                returnfalse;            }        }        elseif(!property1.equals(other.property1)) {            returnfalse;        }        if(property2 != other.property2) {            returnfalse;        }        returntrue;    }} | 
you would first use ConstructorSignatureAndPropertiesMapping to Define the mapping between the constructors' signatures and the properties and then follow the same steps. E.g.:
| // 1. Define the mapping between the constructors' signatures and the properties:ConstructorSignatureAndPropertiesMapping mapping = newConstructorSignatureAndPropertiesMapping();List<Class<?>> signature1 = Arrays.<Class<?>> asList(String.class);mapping.put(signature1, Arrays.asList("property1"));List<Class<?>> signature2 = Arrays.<Class<?>> asList(String.class, int.class);mapping.put(signature2, Arrays.asList("property1", "property2"));// 2. Define the default values expected:PropertiesAndValues defaultValues = newPropertiesAndValues();defaultValues.put("property1", null);defaultValues.put("property2", 0);defaultValues.put("property3", false);defaultValues.put("aReadOnlyProperty", Integer.MAX_VALUE);// 3. Give another value for each of the properties:PropertiesAndValues otherValues = newPropertiesAndValues();otherValues.put("property1", "aNewString");otherValues.put("property2", 3);otherValues.put("property3", true);// 'property4' is a read only property and can be omitted here.// 4. Create the tester:BeanLikeTester blt = newBeanLikeTester(MyBeanLike.class, mapping);// 5. Test the object:blt.testBeanLike(defaultValues, otherValues); |