Search code examples
javaeclipsespring-bootjunit4

How can I junit test the last block of this equal?


How can I JUnit test the last block of this equal?

Any help would be much appreciated. This is the method in question:

@Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    //unreachable block
    ServiceOrderRelationship serviceOrderRelationship = (ServiceOrderRelationship) o;
    return Objects.equals(this.id, serviceOrderRelationship.id) &&
        Objects.equals(this.href, serviceOrderRelationship.href) &&
        Objects.equals(this.relationshipType, serviceOrderRelationship.relationshipType) &&
        Objects.equals(this.baseType, serviceOrderRelationship.baseType) &&
        Objects.equals(this.schemaLocation, serviceOrderRelationship.schemaLocation) &&
        Objects.equals(this.type, serviceOrderRelationship.type) &&
        Objects.equals(this.referredType, serviceOrderRelationship.referredType);
  }

This is what I've been doing but I can never reach the last block of code inside the equals method:

@Test
public void testEquals() throws Exception {

    assertFalse(serviceOrderRelationship.equals(null));
    assertTrue(serviceOrderRelationship.equals(serviceOrderRelationship));
    assertFalse(serviceOrderRelationship.equals(serviceOrderRelationship1));
} 

Solution

  • First of all, thank you all for your responses!

    This is how I was able to reach the last block of the equals method. I had to initialize both objects di and di1 and set every variable to the same value, then test the equals condition switching back and forth one variable at a time to a different value. This is an example from another POJO:

        // Initialize objects
        di.setEdgeId("edgeId");
        di.setIdentityEndpoint("identityEndpoint");
        di.setUsername("username");
    
        di1.setEdgeId("edgeId");
        di1.setIdentityEndpoint("identityEndpoint");
        di1.setUsername("username");
    
        // Change value of var and test equal
        di1.setEdgeId("edgeIdm");
        assertFalse(di.equals(di1));
        di1.setEdgeId("edgeId");
    
        // same
        di1.setIdentityEndpoint("identityEndpointm");
        assertFalse(di.equals(di1));
        di1.setIdentityEndpoint("identityEndpoint");
    
        // Same
        di1.setUsername("usernamem");
        assertFalse(di.equals(di1));
        di1.setUsername("username");
    
        // Then at the end perform the other tests
        assertTrue(di.equals(di));
        assertTrue(di.equals(di1));
        assertFalse(di.equals(null));
        assertFalse(di.equals(42));