org.outsideMyBox.testUtils
Class BeanLikeTester

java.lang.Object
  extended by org.outsideMyBox.testUtils.BeanLikeTester

public final class BeanLikeTester
extends Object

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 not 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.


Nested Class Summary
static class BeanLikeTester.ConstructorSignatureAndPropertiesMapping
          Convenient class to define a list of constructor signatures with their corresponding property name.
static class BeanLikeTester.PropertiesAndValues
          Convenient class to define a list of property names with their corresponding value.
 
Constructor Summary
BeanLikeTester(Class<?> beanClass)
          Create a BeanLikeTester with a specific bean to test.
BeanLikeTester(Class<?> beanLikeClass, BeanLikeTester.ConstructorSignatureAndPropertiesMapping constructorsSignaturesAndProperties)
          Create a BeanLikeTester with a specific beanLike to test.
 
Method Summary
 void testBeanLike(BeanLikeTester.PropertiesAndValues propsWithDefaultValue, BeanLikeTester.PropertiesAndValues propsWithOtherValue)
          Run all the tests: testDefaultValues(PropertiesAndValues) testMutatorsAndAccessors(PropertiesAndValues, PropertiesAndValues) testEqualsAndHash(PropertiesAndValues, PropertiesAndValues) testToString(PropertiesAndValues, PropertiesAndValues)
 void testDefaultValues(BeanLikeTester.PropertiesAndValues expectedDefaultValues)
          Test that the default value of the properties (returned by the accessors) are the same as the one defined by the parameter.
The properties are also tested for objects created with all the possible constructors.
 void testEqualsAndHash(BeanLikeTester.PropertiesAndValues propsWithDefaultValue, BeanLikeTester.PropertiesAndValues propsWithOtherValue)
          Test that equals() and hashCode() take into account all the properties and return the correct values.
 void testMutatorsAndAccessors(BeanLikeTester.PropertiesAndValues propsWithValue, BeanLikeTester.PropertiesAndValues otherPropsWithValue)
          Test that all the mutators (setters, and constructors with arguments) can change their property and that the accessors reflect the change.
 void testToString(BeanLikeTester.PropertiesAndValues propsWithDefaultValue, BeanLikeTester.PropertiesAndValues propsWithOtherValue)
          Test that the method toString() returns a different String if one of the settable properties has changed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanLikeTester

public BeanLikeTester(Class<?> beanLikeClass,
                      BeanLikeTester.ConstructorSignatureAndPropertiesMapping constructorsSignaturesAndProperties)
Create a BeanLikeTester with a specific beanLike to test.

Parameters:
beanLikeClass - The 'beanLike' to test.
constructorsSignaturesAndProperties - The signature of all the possible constructors (The parameters of the constructors must only set properties).
key: constructor's signature. value: corresponding property name.
For beans the map can be empty or null.
Throws:
BeanLikeTesterException - if at least one of the signatures doesn't correspond to a constructor or the constructors doesn't only define properties or setters or accessors are invalid.

BeanLikeTester

public BeanLikeTester(Class<?> beanClass)
Create a BeanLikeTester with a specific bean to test.

Parameters:
beanClass - The bean class to test.
Throws:
BeanLikeTesterException - See BeanLikeTester(Class, ConstructorSignatureAndPropertiesMapping)
Method Detail

testDefaultValues

public void testDefaultValues(BeanLikeTester.PropertiesAndValues expectedDefaultValues)
Test that the default value of the properties (returned by the accessors) are the same as the one defined by the parameter.
The properties are also tested for objects created with all the possible constructors.

Parameters:
expectedDefaultValues - Property names and their expected default value.
Throws:
BeanLikeTesterException - if the test fails.
i.e. if one of the values returned by one of the accessors is different from the expected default value or the properties defined by 'expectedDefaultValues' don't correspond to the beanLike properties.

testMutatorsAndAccessors

public void testMutatorsAndAccessors(BeanLikeTester.PropertiesAndValues propsWithValue,
                                     BeanLikeTester.PropertiesAndValues otherPropsWithValue)
Test that all the mutators (setters, and constructors with arguments) can change their property and that the accessors reflect the change.

Parameters:
propsWithValue - Property names (keys) and their value.
It must at least contain all the settable properties.
otherPropsWithValue - Property names (keys) and their value different from 'propsWithValue'.
It must at least contain all the settable properties.
Throws:
BeanLikeTesterException - if the test fails.

testEqualsAndHash

public void testEqualsAndHash(BeanLikeTester.PropertiesAndValues propsWithDefaultValue,
                              BeanLikeTester.PropertiesAndValues propsWithOtherValue)
Test that equals() and hashCode() take into account all the properties and return the correct values.

Parameters:
propsWithDefaultValue - Property names (keys) and their value.
It must at least contain all the settable properties.
propsWithOtherValue - Property names (keys) and their value different from 'propsWithValue'.
It must at least contain all the settable properties.
Throws:
BeanLikeTesterException - if the test fails.

testToString

public void testToString(BeanLikeTester.PropertiesAndValues propsWithDefaultValue,
                         BeanLikeTester.PropertiesAndValues propsWithOtherValue)
Test that the method toString() returns a different String if one of the settable properties has changed.

Parameters:
propsWithDefaultValue - Property names (keys) and their value.
It must at least contain all the settable properties.
propsWithOtherValue - Property names (keys) and their value different from 'propsWithDefaultValue'.
It must at least contain all the settable properties.
Throws:
BeanLikeTesterException - if the test fails.

testBeanLike

public void testBeanLike(BeanLikeTester.PropertiesAndValues propsWithDefaultValue,
                         BeanLikeTester.PropertiesAndValues propsWithOtherValue)
Run all the tests:

Parameters:
propsWithDefaultValue - Property names (keys) and their value.
It must at least contain all the settable properties.
propsWithOtherValue - Property names (keys) and their value different from 'propsWithValue'.
It must at least contain all the settable properties.
Throws:
BeanLikeTesterException - if the test fails.


Copyright © 2011. All Rights Reserved.