班级学号姓名实验三.docx
《班级学号姓名实验三.docx》由会员分享,可在线阅读,更多相关《班级学号姓名实验三.docx(16页珍藏版)》请在冰豆网上搜索。
班级学号姓名实验三
软件测试实验报告--实验三
班级:
学号:
姓名:
指导老师:
一、实验题目
编制并运行JUnit测试用例程序
二、实验内容
●基于JUnit实现白盒测试用例
●记录测试结果,截屏或文字
三、实验步骤
3.1NetBeans开发环境中建立第一个Junit测试程序,测试数据库的连接如图示正在下载Junit测试插件
3.2Junit测试插件安装好后,建立数据库测试程序,运行结果如下图:
3.3数据库连接测试成功截图如下:
数据库测试程序源代码:
(对应DatabaseConn.java)
packagecom.ustc.biz;
importjava.sql.Connection;
importjunit.framework.TestCase;
publicclassDatabaseConnJUnitTestextendsTestCase{
publicDatabaseConnJUnitTest(StringtestName){
super(testName);
}
protectedvoidsetUp()throwsException{
System.out.println("测试开始");
}
protectedvoidtearDown()throwsException{
System.out.println("测试结束");
}
publicvoidtestGetConnection_0args()throwsException{
System.out.println("getConnection");
System.out.println("------------------------------------");
ConnectionexpResult=null;
Connectionresult=DatabaseConn.getConnection();
assertEquals(expResult,result);
System.out.println(result.toString());
//TODOreviewthegeneratedtestcodeandremovethedefaultcalltofail.
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetConnection_3args()throwsException{System.out.println("——————————————getConnection——————————————");
Stringuserid="root";
Stringpwd="123456";
StringconnectURL="jdbc:
mysql:
//localhost:
3306/schadmin?
useUnicode=true&characterEncoding=UTF-8";
ConnectionexpResult=null;
Connectionresult=DatabaseConn.getConnection(userid,pwd,connectURL);
assertEquals(expResult,result);
System.out.println("——————————————getConnection——————————————");
//TODOreviewthegeneratedtestcodeandremovethedefaultcalltofail.
fail("Thetestcaseisaprototype.");
}
publicvoidtestMain(){
System.out.println("main");
System.out.println("——————————————mainStart——————————————");
String[]agrs=null;
System.out.println("——————————————mainEnd——————————————");
//TODOreviewthegeneratedtestcodeandremovethedefaultcalltofail.
fail("Thetestcaseisaprototype.");
}
}
3.4学生成绩添加测试程序出现空指针异常
错误原因:
编写测试用例期间,对象定义时未申请空间
根据错误提示:
解除引用空值指针
正确的修改方案:
GradeVOgradeVO=null;GradeVOgradeVO=newGradeVO();
测试程序运行成功!
3.5运行编写的测试用例,发现学生的成绩可以添加为负数等,功能异常
学生成绩管理Junit测试源代码:
(对应GradeMgr.java)
packagecom.ustc.biz;
importcom.ustc.vo.GradeVO;
importjava.util.ArrayList;
importjunit.framework.TestCase;
publicclassGradeMgrJUnitTestextendsTestCase{
publicGradeMgrJUnitTest(StringtestName){
super(testName);
}
protectedvoidsetUp()throwsException{
System.out.println("测试开始");
}
protectedvoidtearDown()throwsException{
System.out.println("测试结束");
}
publicvoidtestAddGrade()throwsException{
System.out.println("addGrade");
GradeVOgradeVO=null;
gradeVO.setStuID("20131601");
gradeVO.setNumElectron(60);
gradeVO.setNumSoftware(80);
gradeVO.setNumSecurity(60);
gradeVO.setNumJava(60);
gradeVO.setNumDB(80);
gradeVO.setNumImage(60);
gradeVO.setNumDistributed(60);
gradeVO.setNumTest(80);
GradeMgrinstance=newGradeMgr();
instance.addGrade(gradeVO);
fail("Thetestcaseisaprototype.");
}
publicvoidtestUpdateGrade()throwsException{
System.out.println("updateGrade");
GradeVOgradeVO=null;
gradeVO.setStuID("20131601");
gradeVO.setNumElectron(90);
gradeVO.setNumSoftware(90);
gradeVO.setNumSecurity(90);
gradeVO.setNumJava(90);
gradeVO.setNumDB(90);
gradeVO.setNumImage(90);
gradeVO.setNumDistributed(90);
gradeVO.setNumTest(90);
GradeMgrinstance=newGradeMgr();
instance.updateGrade(gradeVO);
fail("Thetestcaseisaprototype.");
}
publicvoidtestDeleteGradeList()throwsException{
System.out.println("deleteGradeList");
StringSTUID="20131601";
GradeMgrinstance=newGradeMgr();
instance.deleteGradeList(STUID);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetAllStudentsGrade()throwsException{
System.out.println("getAllStudentsGrade");
GradeMgrinstance=newGradeMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getAllStudentsGrade();
if(result==null){
System.out.println("—————————————空—————————————");
}
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetGradeByStudentID()throwsException{
System.out.println("getGradeByStudentID");
Stringstuid="20131601";
GradeMgrinstance=newGradeMgr();
GradeVOexpResult=null;
GradeVOresult=instance.getGradeByStudentID(stuid);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetSearchStudentsGrade()throwsException{
System.out.println("getSearchStudentsGrade");
intsearchkey=60;//及格
GradeMgrinstance=newGradeMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getSearchStudentsGrade(searchkey);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetFirstStudentsGrade()throwsException{
System.out.println("getFirstStudentsGrade");
intsearchkey=100;
GradeMgrinstance=newGradeMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getFirstStudentsGrade(searchkey);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetSearchStudentsGradebyID()throwsException{
System.out.println("getSearchStudentsGradebyID");
Stringsearchkey="20131601";
GradeMgrinstance=newGradeMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getSearchStudentsGradebyID(searchkey);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestDeleteGrade()throwsException{
System.out.println("deleteGrade");
StringSTUID="20131601";
GradeMgrinstance=newGradeMgr();
instance.deleteGrade(STUID);
fail("Thetestcaseisaprototype.");
}
publicvoidtestDeleteAllGrade()throwsException{
System.out.println("deleteAllGrade");
GradeMgrinstance=newGradeMgr();
instance.deleteAllGrade();
fail("Thetestcaseisaprototype.");
}
publicvoidtestIsExistSTUIDGrade()throwsException{
System.out.println("isExistSTUIDGrade");
StringstuID="20131601";
GradeMgrinstance=newGradeMgr();
booleanexpResult=false;
booleanresult=instance.isExistSTUIDGrade(stuID);
assertEquals(expResult,result);
//TODOreviewthegeneratedtestcodeandremovethedefaultcalltofail.
fail("Thetestcaseisaprototype.");
}
publicvoidtestMain(){
System.out.println("main");
String[]args=null;
GradeMgr.main(args);
fail("Thetestcaseisaprototype.");
}
}
学生信息管理Junit测试源代码:
(对应StudentMgr.java)
packagecom.ustc.biz;
importcom.ustc.vo.StudentVO;
importjava.util.ArrayList;
importjunit.framework.TestCase;
publicclassStudentMgrTestextendsTestCase{
publicStudentMgrTest(StringtestName){
super(testName);
}
protectedvoidsetUp()throwsException{
System.out.println("测试开始");
}
protectedvoidtearDown()throwsException{
System.out.println("测试结束");
}
publicvoidtestAddStudent()throwsException{
System.out.println("addStudent");
StudentVOstudentVO=null;
studentVO.setStuID("2301445");
studentVO.setUsername("2301445");
studentVO.setGender("男");
studentVO.setCity("2301");
studentVO.setIdentityID("230144523014451");
studentVO.setPhone("2301445");
studentVO.setEmail("**************");
studentVO.setPostID("230144");
studentVO.setAddress("2301445");
StudentMgrinstance=newStudentMgr();
instance.addStudent(studentVO);
fail("Thetestcaseisaprototype.");
}
publicvoidtestUpdateStudent()throwsException{
System.out.println("updateStudent");
StudentVOstudentVO=null;
studentVO.setStuID("2301445");
studentVO.setUsername("mmmmmm");
studentVO.setGender("男");
studentVO.setCity("2301");
studentVO.setIdentityID("230144523014451");
studentVO.setPhone("mmmmmm");
studentVO.setEmail("**************");
studentVO.setPostID("mmmmmm");
studentVO.setAddress("mmmmmm");
StudentMgrinstance=newStudentMgr();
instance.updateStudent(studentVO);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetAllStudents()throwsException{
System.out.println("getAllStudents");
StudentMgrinstance=newStudentMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getAllStudents();
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetUserNameByID()throwsException{
System.out.println("getUserNameByID");
Stringstuid="2301445";
StudentMgrinstance=newStudentMgr();
StringexpResult="";
Stringresult=instance.getUserNameByID(stuid);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetSearchStudents()throwsException{
System.out.println("getSearchStudents");
Stringsearchkey="";
StudentMgrinstance=newStudentMgr();
ArrayListexpResult=null;
ArrayListresult=instance.getSearchStudents(searchkey);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGetStudentByID()throwsException{
System.out.println("getStudentByID");
Stringstuid="2301445";
StudentMgrinstance=newStudentMgr();
StudentVOexpResult=null;
StudentVOresult=instance.getStudentByID(stuid);
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}
publicvoidtestDeleteStudent()throwsException{
System.out.println("deleteStudent");
StringSTUID="2301445";
StudentMgrinstance=newStudentMgr();
instance.deleteStudent(STUID);
fail("Thetestcaseisaprototype.");
}
publicvoidtestGenerateSTUID()throwsException{
System.out.println("generateSTUID");
StudentMgrinstance=newStudentMgr();
StringexpResult="";
Stringresult=instance.generateSTUID();
assertEquals(expResult,result);
fail("Thetestcaseisaprototype.");
}