Tuesday, May 21, 2013

easier testing with unitils

Many times in my unit tests I needed to assert that two objects equals while they didn't implement equals method or the equals method wasn't appropriate to ensure that these object are logically the same. So the obvious solution was to write several asserts comparing particular fields from given objects:

Today I came across Unitils, a great library that facilitates writing tests in Java. It consists of several modules, but the most interesting for me is Reflection Assert. It provides an assertReflectionEquals method that verifies if two objects are equal - field by field, recursively.

That's it! Isn't it easy?

Additionally it gives a detailed info when assertion fails. Here is samle output: