Mean Bean


4 November 2013, by

Recently, on one of Softwire’s projects, we were tasked with retrofitting a test suite to a codebase. The use of Hibernate on the project had resulted in a large number of getters and setters, and we wanted an easy way to check for obvious bugs in this code. MeanBean:

  • Tests that the getter and setter method pairs of a JavaBean/POJO function correctly.
  • Verifies that the equals and hashCode methods of a class comply with the Equals Contract and HashCode Contract respectively.
  • Verifies property significance in object equality.


We only made use of the first point feature on this project.

Mean Bean works by reflecting all of the getter and setter pairs from the class you pass it. Setup is very simple. Pass your class to a BeanTester and it will test the correct methods multiple times using a range of randomly generated values. The interface is also developed to allow non standard situations – for example getting/setting non trivial types such as jodatime.

In order to automate the process further, MeanBean can be combined with Reflections to test all classes within a package, which in this case resulted in a large boost to code coverage, meaning we could be more confident that our code-database interface was set up correctly.

Alternatives:

  1. Don’t use getters and setters (If they’re always going to do the default basic function, why add all the extra code in to handle this).
  2. Use projectlombok, which will automatically create your getters and setters without cluttering your class.
  3. Don’t test your getters and setters (a bit risky, as they can contain bugs).
  4. Test your getters and setters as part of your normal unit tests.

Option 4 is, in my opinion, a better solution. Therefore, if someone is starting a new project, I wouldn’t recommend MeanBean. However on an older project, where code coverage is very low, this is a simple way to ensure that there aren’t any obvious bugs in some of your existing code, but this should ideally only be a temporary solution until these methods are tested by more context relevant unit tests.

Tags: , , ,

Categories: Softwire, Technical

«
»

Leave a Reply

* Mandatory fields


− 2 = two

Submit Comment