Unit Testing Equals and HashCode of Java Beans

copy from http://blog.cornetdesign.com/2008/05/unit-testing-equals-and-hashcode-of-java-beans/

 

Posted on May 28th, 2008

 

We’ve been creating several Java Beans that have an imperative need to have both equals and hashCode working correctly. To do this, we started off writing a series of test cases which made sure that for every field we exposed that the objects were / were not equal based on if the field was set, and that the field was taken into account as part of the hashCode calculation. After doing two of these, I figured there had to be a better way, and after some research, ended up writing a BeanTestCase class which exposes an assertMeetsEqualsContract method and an assertMeetsHashCodeContract method. Note that while there are projects out there (like Assertion Extensions for JUnit ) that have assertions for equals and hashCode, they don’t actually walk the fields of the objects and test the various scenarios of changing each field.

After I had written this, I applied it to classes we already had unit tests for around equals and hashCode – and immediately found a bug in one of the classes where we missed a field. That paid for itself right away. ;)

Being that I’m just getting back into the Java world, if there are better ways, please let me know!

package com.cornetdesign;

import junit.framework.TestCase;

import java.lang.reflect.Field;

public class BeanTestCase extends TestCase {

    private static final String TEST_STRING_VAL1 = "Some Value";
    private static final String TEST_STRING_VAL2 = "Some Other Value";

    public static void assertMeetsEqualsContract(Class classUnderTest) {
        Object o1;
        Object o2;
        try {
            //Get Instances
            o1 = classUnderTest.newInstance();
            o2 = classUnderTest.newInstance();

            assertTrue("Instances with default constructor not equal (o1.equals(o2))", o1.equals(o2));
            assertTrue("Instances with default constructor not equal (o2.equals(o1))", o2.equals(o1));

            Field[] fields = classUnderTest.getDeclaredFields();
            for(int i = 0; i < fields.length; i++) {

                //Reset the instances
                o1 = classUnderTest.newInstance();
                o2 = classUnderTest.newInstance();

                Field field = fields[i];
                field.setAccessible(true);
                if(field.getType() == String.class) {
                    field.set(o1, TEST_STRING_VAL1);
                }  else if(field.getType() == boolean.class) {
                    field.setBoolean(o1, true);
                }   else if(field.getType() == short.class) {
                    field.setShort(o1, (short)1);
                }   else if(field.getType() == long.class) {
                    field.setLong(o1, (long)1);
                }   else if(field.getType() == float.class) {
                    field.setFloat(o1, (float)1);
                }   else if(field.getType() == int.class) {
                    field.setInt(o1, 1);
                }   else if(field.getType() == byte.class) {
                    field.setByte(o1, (byte)1);
                }   else if(field.getType() == char.class) {
                    field.setChar(o1, (char)1);
                }   else if(field.getType() == double.class) {
                    field.setDouble(o1, (double)1);
                }   else if(field.getType().isEnum()) {
                    field.set(o1, field.getType().getEnumConstants()[0]);
                } else if(Object.class.isAssignableFrom(field.getType())) {
                    field.set(o1, field.getType().newInstance());
                }   else {
                    fail("Don't know how to set a " + field.getType().getName());
                }

                assertFalse("Instances with o1 having " + field.getName() + " set and o2 having it not set are equal", o1.equals(o2));


                field.set(o2, field.get(o1));

                assertTrue("After setting o2 with the value of the object in o1, the two objects in the field are not equal"
                        , field.get(o1).equals(field.get(o2)));

                assertTrue("Instances with o1 having "
                        + field.getName()
                        + " set and o2 having it set to the same object of type "
                        + field.get(o2).getClass().getName()
                        + " are not equal", o1.equals(o2));
                
                if(field.getType() == String.class) {
                    field.set(o2, TEST_STRING_VAL2);
                }  else if(field.getType() == boolean.class) {
                    field.setBoolean(o2, false);
                }   else if(field.getType() == short.class) {
                    field.setShort(o2, (short)0);
                }   else if(field.getType() == long.class) {
                    field.setLong(o2, (long)0);
                }   else if(field.getType() == float.class) {
                    field.setFloat(o2, (float)0);
                }   else if(field.getType() == int.class) {
                    field.setInt(o2, 0);
                }   else if(field.getType() == byte.class) {
                    field.setByte(o2, (byte)0);
                }   else if(field.getType() == char.class) {
                    field.setChar(o2, (char)0);
                }   else if(field.getType() == double.class) {
                    field.setDouble(o2, (double)1);
                }   else if(field.getType().isEnum()) {
                    field.set(o2, field.getType().getEnumConstants()[1]);
                } else if(Object.class.isAssignableFrom(field.getType())) {
                    field.set(o2, field.getType().newInstance());
                }   else {
                    fail("Don't know how to set a " + field.getType().getName());
                }
                if(field.get(o1).equals(field.get(o2)))  {
                    //Even though we have different instances, they are equal. Let's walk one of them
                    //to see if we can find a field to set
                    Field[] paramFields = field.get(o1).getClass().getDeclaredFields();
                    for(int j=0; j < paramFields.length; j++) {
                        paramFields[j].setAccessible(true);
                        if(paramFields[j].getType() == String.class) {
                            paramFields[j].set(field.get(o1), TEST_STRING_VAL1);
                        }
                    }
                }

                assertFalse("After setting o2 with a different object than what is in o1, the two objects in the field are equal. "
                        + "This is after an attempt to walk the fields to make them different"
                        , field.get(o1).equals(field.get(o2)));
                assertFalse("Instances with o1 having " + field.getName() + " set and o2 having it set to a different object are equal", o1.equals(o2));
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
            throw new AssertionError("Unable to construct an instance of the class under test");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new AssertionError("Unable to construct an instance of the class under test");
        }
    }

    public void testEqualsContractMet() {
        assertMeetsEqualsContract(FakeObject.class);
    }

    public static void assertMeetsHashCodeContract(Class classUnderTest) {
        try {
            Field[] fields = classUnderTest.getDeclaredFields();
            for(int i = 0; i < fields.length; i++) {
                Object o1 = classUnderTest.newInstance();
                int initialHashCode = o1.hashCode();

                Field field = fields[i];
                field.setAccessible(true);
                if(field.getType() == String.class) {
                    field.set(o1, TEST_STRING_VAL1);
                }  else if(field.getType() == boolean.class) {
                    field.setBoolean(o1, true);
                }   else if(field.getType() == short.class) {
                    field.setShort(o1, (short)1);
                }   else if(field.getType() == long.class) {
                    field.setLong(o1, (long)1);
                }   else if(field.getType() == float.class) {
                    field.setFloat(o1, (float)1);
                }   else if(field.getType() == int.class) {
                    field.setInt(o1, 1);
                }   else if(field.getType() == byte.class) {
                    field.setByte(o1, (byte)1);
                }   else if(field.getType() == char.class) {
                    field.setChar(o1, (char)1);
                }   else if(field.getType() == double.class) {
                    field.setDouble(o1, (double)1);
                }   else if(field.getType().isEnum()) {
                    field.set(o1, field.getType().getEnumConstants()[0]);
                } else if(Object.class.isAssignableFrom(field.getType())) {
                    field.set(o1, field.getType().newInstance());
                }   else {
                    fail("Don't know how to set a " + field.getType().getName());
                }
                int updatedHashCode = o1.hashCode();
                assertFalse("The field " + field.getName() + " was not taken into account for the hashCode contract ", initialHashCode == updatedHashCode);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
            throw new AssertionError("Unable to construct an instance of the class under test");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new AssertionError("Unable to construct an instance of the class under test");
        }
    }

    public void testHashCodeContractMet() {
        assertMeetsHashCodeContract(FakeObject.class);
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值