如何在 Android 中使用 JUnit 测试模型?

2 java android unit-testing android-activity

下面我有一个我在 Android Studio 中创建的模型示例,用于在我的应用程序中使用。

有这方面经验的人可以给我提供我可以在这个模型(在android中)上进行的单元测试的例子来让我开始吗?

- 我指的是通过 Google Android 测试进行的 JUnit 测试

使用(扩展)TestCases ( junit.framework) 等为 JUnit 测试创建函数

接触模型的代码:

public class Contact extends MainModel
{
    private Long id;
    private String personName;
    private String phoneNumber;
    private String occupation;



    public Long getId() 
    { 
        return id; 
    }

    public void setId(Long id)
    { 
        this.id = id; 
    }



    public String getPersonName() 
    { 
        return personName; 
    }

    public void setPersonName(String personName) 
    { 
        this.personName = personName; 
    }



    public String getPhoneNumber()
    { 
        return phoneNumber; 
    }

    public void setPhoneNumber(String phoneNumber) 
    { 
        this.phoneNumber = phoneNumber; 
    }



    public String getOccupation()
    {
        return occupation;
    }

    public void setOccupation(String occupation)
    {
        this.occupation = occupation;
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 6

最近一直在研究 Android 中的类似 junit 测试,这应该会让你开始。它应该解释如何测试获取和设置

public class SurveyTest extends TestCase {

private Survey survey;

protected void setUp() throws Exception {
    super.setUp();  
    survey = new Survey();
}

public void testSurvey() {
    survey.toString();
}

public void testSurveyLongString() {
    fail("Not yet implemented");
}

public void testGetId() {
    long expected = (long) Math.random();
    survey.setId(expected);
    long actual = survey.getId();
    Assert.assertEquals(expected, actual);
}

public void testGetTitle() {
    String expected = "surveytitle";
    survey.setTitle(expected);
    String actual = survey.getTitle();
    Assert.assertEquals(expected, actual);  
}

public void testIsActive() {
    Boolean expected = true;
    survey.setActive(expected);
    Boolean actual = survey.isActive();
    Assert.assertEquals(expected, actual);
}

public void testGetQuestions() {
    fail("Not yet implemented");
}

}
Run Code Online (Sandbox Code Playgroud)