Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage incomplete in hashcode #4

Open
orien opened this issue May 24, 2015 · 0 comments
Open

Test coverage incomplete in hashcode #4

orien opened this issue May 24, 2015 · 0 comments

Comments

@orien
Copy link
Owner

orien commented May 24, 2015

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.

@orien orien changed the title Test coverage incomplete in hashcode : AbstractBeanHashCodeMatcher.hashCodeIsInfluencedByProperties does not check 1st property with null value Test coverage incomplete in hashcode Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant