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

标签 java android unit-testing android-activity

下面是我在 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/

相关文章:

java - 如何在单次 JVM 运行中重新启动 DataNucleus?

java - 根据数字程序设置GridView的ImageVIew

java - 我通过 Java 连接到 MySQL,但它不正确。谁能帮我?

java - 如何使用 Java Swing 布局管理器来制作这个 GUI?

android - 扑克牌类型库

java - Android 中的 Firebase 自动空对象引用

java - 致命异常 : main android eclipse

安卓小部件 : Unable to instantiate activity ComponentInfo, java.lang.ClassCastException

python - 向大型 python 项目添加单元测试

javascript - 两个 Controller 测试套件,其中一个给出 "Error: [ng:areq] Argument ' modalController' 不是函数,未定义”