我希望Junit代码也以100%的分支覆盖范围覆盖模型类。但是,无论我在内什么都不会覆盖100%的

问题描述 投票:0回答:1

@NoArgsConstructor @Data public class MyDetailsEntity extends Entity { private String myDetailsIdentifier; private String approvedDetailsIdentifier; }

贝洛是样本junit测试案例:
public class MyDetailsEntityTest {
    @Test
    public void testNoArgsConstructor() {
        // Explicitly testing the no-args constructor
        MyDetailsEntity entity = new MyDetailsEntity();
        assertNotNull(entity);
    }

    @Test
    public void testLombokGeneratedMethods() {
        MyDetailsEntity entity1 = new MyDetailsEntity();
        entity1.setMyDetailsIdentifier("UL123");
        entity1.setApprovedApplicationIdentifier("APP123");

        // Validate getters
        assertEquals("UL123", entity1.getMyDetailsIdentifier());
        assertEquals("APP123", entity1.getApprovedDetailsIdentifier());

        // Test equals and hashCode
        MyDetailsEntity entity2 = new MyDetailsEntity();
        entity2.setMyDetailsIdentifier("UL123");
        entity2.setApprovedApplicationIdentifier("APP123");

        assertEquals(entity1, entity2);
        assertEquals(entity1.hashCode(), entity2.hashCode());

        // Test equals with itself
        assertEquals(entity1, entity1);

        // Test equals with null
        assertNotEquals(entity1, null);

        // Test equals with a different class
        assertNotEquals(entity1, new Object());

        // Test hashCode with null fields
        assertNotEquals(entity1.hashCode(), entity3.hashCode());
        assertEquals(entity3.hashCode(), entity3.hashCode());

        // Test toString for null and non-null values
        assertNotNull(entity1.toString());
        assertNotNull(entity3.toString());
        assertTrue(entity1.toString().contains("UL123"));
        assertFalse(entity3.toString().contains("UL123"));
    }
}

还需要包括什么才能覆盖分支机构的100%?

有另一种方法,您可以配置Lombok以将

@Generated

注释添加到其生成的每种方法中。默认情况下,覆盖范围工具将从覆盖范围中排除
@Generated
方法。

要配置Lombok以添加

@Generated
java junit lombok
1个回答
0
投票
lombok.config

lombok.addLombokGeneratedAnnotation = true

https://www.baeldung.com/lombok-configuration-system

https://projectlombok.org/features/configuration

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.