Skip to content

Test coverage incomplete in hashcode #4

@orien

Description

@orien

migrated from google code issue 1

AbstractBeanHashCodeMatcher.hashCodeIsInfluencedByProperties does not check 1st property with null value. The test coverage is incomplete.

What steps will reproduce the problem?

  1. Create a java bean with at least two object properties. E.g.

    package com.acme.dto;
    
    public class Name {
        private String firstName;
        private String lastName;
    
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return this.lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((this.firstName == null) ? 0 : this.firstName.hashCode());
            result = prime * result + ((this.lastName == null) ? 0 : this.lastName.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            Name other = (Name) obj;
            if (this.firstName == null) {
                if (other.firstName != null) {
                    return false;
                }
            }
            else if (!this.firstName.equals(other.firstName)) {
                return false;
            }
            if (this.lastName == null) {
                if (other.lastName != null) {
                    return false;
                }
            }
            else if (!this.lastName.equals(other.lastName)) {
                return false;
            }
            return true;
        }
    }
  2. In eclipse, using the eclemma test coverage tool (or simply using the debugger in "Name" hashCode): run the following unit test:

    @Test
    public void testName() {
        assertThat(Name.class, hasValidBeanHashCode());
        assertThat(Name.class, hasValidGettersAndSetters());
        assertThat(Name.class, hasValidBeanEquals());
    }

What is the expected output? What do you see instead?

Look at the covered lines and you will notice there is one branch missing coverage for the 1st property of the bean: this is because the value generator checks for two values for all beans but it does not start with a hashcode test without any value which means the case where all fields are null is not tested, hence the missed branch.

Now, add this test:

@Test
public void testNameHashcodeAllNulls() {
    Name name = new Name();
    int hashCode = name.hashCode();
    assertNotEquals(0, hashCode);
}

Run again and the coverage is now 100%

What version of the product are you using? On what operating system?

bean-matchers 0.9

Please provide any additional information below.

Of course, if the first property is a basic type such as an int, the coverage will ok. This is a workaround for some beans. The real issue happens when the bean only has objects like the "Name" example.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions