下面是我在 Android Studio 中创建的要在我的应用程序中使用的模型示例。
有这方面经验的人可以给我一些单元测试的例子吗,我可以在这个模型(在 android 中)上进行,让我开始?
- 我指的是通过 Google Android 测试进行的 JUnit 测试
利用(扩展)TestCases (junit.framework
) 等为 JUnit 测试创建函数
Contact 模型的代码:
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;
}
}
最佳答案
最近一直在研究 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");
}
}
关于java - 我如何在 Android 中使用 JUnit 测试模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113528/