Tests normal bean methods such as accessors, mutators, equals(), hashcode(), etc.
Clone or download
Latest commit 6430263 Sep 28, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.settings Refactored value generation for classes associated with primitives Mar 29, 2015
src boost test coverage Apr 11, 2015
.classpath added .classpath to gitignore Apr 5, 2015
.gitignore added .classpath to gitignore Apr 5, 2015
.project initial check-in Mar 28, 2015
LICENSE Initial commit Mar 27, 2015
README.md Install doc bug fix Sep 28, 2016
pom.xml Advance to version 1.0.0 Apr 12, 2015

README.md

BeanTester

Tests normal bean methods such as accessors, mutators, equals(), hashcode(), etc. If your organization has code coverage requirements, testing beans is some of the most monotonous work. It's mindless and the less test code you need to write for this, the better. BeanTester was written to get out of this monotonous work. Predecessors to it have been copy/pasted/improved into almost a dozen projects over the years.

Specifically, the following tests are performed:

  • All accessors (e.g. getXXX() methods) execute without exception
  • All mutators (e.g. setXXX() methods) execute without exception
  • accessors return the value assigned to them via their corresponding mutator (e.g. getXXX() returns the same value submitted with setXXX())
  • An instance equals() itself
  • hashcode() runs without exception
  • hashcode() for an instance is constant over time if no changes are made to it
  • hashcode() equals 0. The pit test product plugs 0 in to determine if you're properly testing hashcode()
  • clone() executes without exception
  • clone() equals the instance from which it was produced
  • compareTo() executes without exception
  • toString() executes without exception
  • toString() is not null. The pit test product plugs null in to determine if you're properly testing
  • An instance compareTo() is 0 when compared to itself
  • An instance serializes and equals a reconstituted version of itself (if java.io.Serializable is implemented)

Additional detail in the 'Usage Instructions and Examples' section.

System Requirements

  • Java JDK 6.0 or above (it was compiled under JDK 7 using 1.6 as the target source).
  • Apache Commons Lang version 3.0 or above
  • Apache Commons Beanutils (tested with 1.9.2 but earlier versions will likely work)

Installation Instructions

BeanTester is easy to install whether you use maven or not. Simply add the deployed jar to your classpath.

Maven Users

Maven users can find dependency information here.

Non-Maven Users

Include the following jars in your class path:

  • Download the BeanTester jar from Github and put it in your class path.
  • Insure Apache Commons Lang version 3.0 or above is in your class path.
  • Insure Apache Commons Beanutils version 1.8 or above is in your class path.

Usage Instructions and Examples

Basic usage example:

BeanTester beanTester = new BeanTester();
beanTester.testBean(PerformanceSummaryVO.class);  

If your bean does not have a null constructor, you must provide construction arguments:

BeanTester beanTester = new BeanTester();
beanTester.testBean(Integer.class, new Object[]{"0"});  

Exclude specific fields from the test if needed.

BeanTester beanTester = new BeanTester();
beanTester.addExcludedField("firstOccuranceDt");
beanTester.addExcludedField("lastOccuranceDt");
beanTester.addExcludedField("totalNbrExceptions");
beanTester.testBean(PerformanceSummaryVO.class);  

Add custom values for property-level testing:

BeanTester beanTester = new BeanTester();
beanTester.addTestValues(PerformanceSummaryVO.class, new Object[]{custom1, custom2});  

BeanTester comes with support for the following data types out of the box:

  • Primitives (both singleton and arrays)
  • Primitive Object Wrappers (both singleton and arrays)
  • Any class type with a public null constructor
  • Enums
  • Temporal types, such as java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp, GregorianCalendar, XmlGregorianCalandar

Remember that you can register test values for any custom class. this means that any data type is supported.

BeanTester does not perform nested testing. That is, it doesn't automatically test classes it encounters used as fields. Tests for those classes should be separate executions of BeanTester.

Adding Custom Bean Tests

Custom bean tests implment interface BeanTest.

This isn't required, but implementing BeanTest is often easier if you extend BaseBeanTest.

Then add your test to the BeanTester configuration before running. An example:

DefaultBeanTesterConfiguration config = new DefaultBeanTesterConfiguration();
config.getBeanTestList().add( new MyCustomTest() );

BeanTester beanTester = new BeanTester(config);
beanTester.testBean(PerformanceSummaryVO.class);  

If you decide to develop custom bean tests that other might find useful, please contribute them back. Just email one of the committers with your suggested addition and we'll consider it.