Fork me on GitHub

Testing the hashCode Method

Matchers for ensuring properties influence the generated hash code. This is tested by setting the property with one value and comparing the generated hash code with that produced when the property is set with a different value. If the values differ we infer the hash code is correctly influenced by the property.

Ensure that all properties on the bean influence the produced hash code.

assertThat(BeanToTest.class, hasValidBeanHashCode());

Ensure that the property named propertyInfluencingHashCode influences the produced hash code.

assertThat(BeanToTest.class, hasValidBeanHashCodeFor("propertyInfluencingHashCode"));

Ensure that all properties on the bean except the property named propertyNotInfluencingHashCode influences the produced hash code.

assertThat(BeanToTest.class, hasValidBeanHashCodeExcluding("propertyNotInfluencingHashCode"));