Testing for Equality using reflection

This utility tests for equality using reflection. Saves you from getting carpal tunnel by  writing multiple lines of the form

assertThat(result.getFoo(), is(equalTo(item.getFoo())));

Here’s the source. Modify as needed

public class MappingTestUtility {

public static void assertEquals(Object input, Object result) throws IllegalAccessException {
 assertThat(result, is(equalTo(input)));
 final Field[] fields = input.getClass().getDeclaredFields();
 for (final Field field : fields) {
 assertThat(field.get(input), is(equalTo(field.get(result))));


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s