本编测试示对项目“数据库的增删改查”的测试,blog:http://blog.csdn.net/ss1168805219/article/details/51276243
单元测试的步骤:
添加jar
声明组件 instrumentation
引用组件 instrumentation
编写测试代码
1 添加jar
右键项目–>property–>java build path–>libraries–>add library–>JUnit–>JUnit4–>finish,会出现JUnit4, 最后order and export :在JUnit前面打钩。结果见下图:
![]()
2 声明组件 instrumentation
AndroidManifest.xml
<!-- 添加测试组件 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.android.sqlitedemo01" >
</instrumentation>
3 引用组件 instrumentation
AndroidManifest.xml(application节点下)
<!--4引用组件 -->
<uses-library
android:name="android.test.runner"
android:required="true" />
4 编写测试代码
1 创建测试类:
右键new –> JUit Test Case见下图:
2 编写测试代码
public class TestStudentDao extends AndroidTestCase {
private static final String tag = "TestStudentDao";
@Test
public void testAdd() {
StudentsDao dao = new StudentsDao(mContext);
for (int i = 0; i < 10; i++) {
StudentBean stu = new StudentBean();
stu.name ="毛泽东"+i;
stu.number = "00"+i;
stu.age = 60+i;
dao.add(stu);
}
}
@Test
public void testFindAll() {
StudentsDao dao = new StudentsDao(mContext);
List<StudentBean> list = dao.findAll();
for (int i = 0; i < list.size(); i++) {
StudentBean stu = list.get(i);
Log.d(tag, "id = "+stu.id+"--name="+stu.name+"--number="+stu.number+"--age"+stu.age);
}
}
@Test
public void testDelete() {
StudentsDao dao = new StudentsDao(mContext);
dao.delete("1");
}
@Test
public void testUpdate() {
StudentsDao dao = new StudentsDao(mContext);
StudentBean stu = new StudentBean();
stu.id = "10";
stu.name = "1";
stu.number="0000";
stu.age = 66;
dao.update(stu);
}
}