Home > Blockchain >  Why am I not covering totaly hashcode test with lombok.Data
Why am I not covering totaly hashcode test with lombok.Data

Time:11-03

I am looking for the reason why jacoco is it showing me that i am not covering totaly my functions equals and hashCode.

I have this model code :

import lombok.Data;

@Data
public class Building2 {
    private String buildingId;
    private String buildingName;

    public Building2(String buildingId, String buildingName) {
        this.buildingId = buildingId;
        this.buildingName = buildingName;
    }
}

and I test like that:

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class ModelTest {
    
    @Test
    void instantiateBuilding2ModelTest(){
        // Given
        String buildingId = "[email protected]";
        String buildingName ="myBuildingName";
        String buildingId2 = "[email protected]";
        String buildingName2 = "secondBuildingName";
        String buildinId3 = "[email protected]";
        String buildingName3 = "thirdBuildingName";
        Building2 building2;
        Building2 building2_2 = new Building2(buildingId2, buildingName2);
        Building2 building2_3 = new Building2(buildinId3, buildingName3);
        

        // Test Getter
        building2 = new Building2(buildingId, buildingName);
        assertEquals(building2.getBuildingId(), buildingId);
        assertEquals(building2.getBuildingName(), buildingName);

        // Test Setter
        building2.setBuildingId(buildingId2);
        building2.setBuildingName(buildingName2);
        assertEquals(building2.getBuildingId(), buildingId2);
        assertEquals(building2.getBuildingName(), buildingName2);

        // Test toString
        String building2ToString = "Building2(buildingId="   building2.getBuildingId()   ", buildingName="   building2.getBuildingName()   ")";
        assertEquals(building2.toString(), building2ToString);

        // Test canEqual
        assertTrue(building2.canEqual(building2_3) && building2_3.canEqual(building2));

        // Test equals
        assertTrue(building2.equals(building2_2));
        assertFalse(building2.equals(building2_3));

        // Test hashCode
        assertTrue(building2.hashCode() == building2_2.hashCode());
        assertFalse(building2.hashCode() == building2_3.hashCode());
    }
}

but when I generate jacoco report for test covering, it's showing me that : jacoco report

so I don't understand what method am I not testing actually.

I begin with springboot and JUnit, so thank you a lot for your answer!

CodePudding user response:

To exclude methods created by Lombok outside of project coverage, you can create a lombok.config file in your root directory and have these two lines:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
  • Related