mardi 19 septembre 2017

ModelClass.class.getDeclaredFields().length returns 4 during Gradle Build but there are 3 declared attributes

I have the following POJO class:

public class ModelClass {

    private String name;
    private String phone;
    private String marks;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMarks() {
        return marks;
    }

    public void setMarks(String marks) {
        this.marks = marks;
    }
}

I have a test method where the requirement is to get total number of attributes declared in the model class at runtime and verify it. Following is the code snippet in my test class:

@Test
public void demoTest() {
    int fields = ModelClass.class.getDeclaredFields().length;
    assertEquals(CUSTOM_EXCEPTION_MESSAGE, fields, 3);
}

This test is running fine in Eclipse when I run it as Junit test. But during the build it fails saying:

java.lang.AssertionError:expected:<4> but was:<3>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)

This is a Gradle Project. Can anyone shed some light on this unusual behaviour?





Aucun commentaire:

Enregistrer un commentaire