发表于: 2017-08-06 17:17:19

3 1077


今天主要是改bug,然后对数据操作类的各功能进行测试,然后根据测试结果进一步完善代码,最后简单了解了下Junit。


1.bug记录
bug1:url错误。
错误原因:①数据库名是students,少了个s;
 ②是正斜杠而不是反斜杠

bug2:Access denied for user 'asus'@'localhost' (using password: YES)
错误定位:db.properties读取(<context:property-placeholder location="classpath:db.properties"/>)发生错误。
错误原因:未明。
修改:将读取db.properties的方式修改为:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:db.properties"></property>
</bean>
即可正常读取。

bug3:Table 'students.student' doesn't exist
错误原因:各sql语句中插入的表名是student,应该为students

bug4:Column 'jnshuId' not found.
错误原因:mapRow中的rs.getString("")引号中应为数据库的列名,而不是Student类中的属性名 。

bug5:打印结果为数组地址
修改:将showStu函数中的sop(obj)改为foreach循环遍历。

Warn:Sat Aug 05 03:09:59 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
原因:未设置数据库的连接属性(是否使用SSL)。
解决:将url修改为:jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&useSSL=false,即可。
2.功能测试
测试前students表内容:

①查询方法的测试:
>通过id查询
	Student stu = test.stuDAO.queryStuById(1);
	test.showStu(stu);
查询结果:

查询一个不存在的id呢?
	Student stu = test.stuDAO.queryStuById(110);
	test.showStu(stu);
结果:

报错了。程序中应该添加对各查询函数查询的学生不存在的问题的处理。
>通过name查询:
	Student stu = test.stuDAO.queryStuByName("胡凯博");
	test.showStu(stu);
结果:

原因:返回了大量同名的学生。这里应该修改代码:和通过id、专业+学号查询不同,确实可能存在同名的学生。所以,通过姓名查询时,应该返回List<Student>而不是Student。
>通过专业+学号查询:
	Student stu = test.stuDAO.queryStuByJnshu("JAVA后端工程师", 1199);
	test.showStu(stu);
结果:

原因:sql语句错误。定位后发现多写了一个where。。。。。。。删除后结果正常。
②更新方法的测试:
	Student stu = test.stuDAO.queryStuById(1);
	test.showStu(stu);
	//改-查看数据库得到结果
	stu.setName("张三");
	test.sop(test.stuDAO.updateStu(stu, 1));
结果:

成功更新。
③插入方法的测试:
	Student stu = test.stuDAO.queryStuById(1);
	test.showStu(stu);	stu.setName("李四");	stu.setJnshuId(7897);
	test.sop(test.stuDAO.addStu(stu));
结果:

成功插入。
④删除方法的测试:
>通过id删除:
test.sop(test.stuDAO.delStuById(15));
结果:

成功删除。通过专业+学号删除结果类似。
>通过姓名删除
test.sop(test.stuDAO.delStuByName("胡凯博"));
结果:

这里返回值是boolean,实际上通过姓名删除可能会删除多条记录,所以返回删除的记录数量比较好,应该修改。


3.完善代码并测试

根据测试结果,需要修改的地方有:
  • 各查询函数中,添加对要查询的学生不存在的情况的处理
  • 通过name查询学生时,返回值改为List<Student>,因为可能有重名学生
  • 通过name删除学生时,返回值改为int,因为可能删除多名学生
通过查看源码,发现queryForObject必须返回且仅返回一个对象,查询时调用query方法,获取List<T>。List为空时抛出EmptyResultDataAccessException异常,List元素多于1个时抛出IncorrectResultSizeDataAccessException异常。

所以,在通过id、专业+学号查询时,通过try-catch捕获异常,给出查询结果为空的提示;通过name查询时,改为调用query方法。
完成修改后,对修改部分进行测试:
①查询不存在的学生:

Student student = test.stuDAO.queryStuById(1111); 

 if (student!=null) 

   test.showStu(student);

结果:

②查询重名的学生:

List<Student> list = test.stuDAO.queryStuByName("李四"); 

if (list.size()!=0) {

 int num = 0; 

 for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) { 

   Student student = (Student) iterator.next(); 

   test.sop("Student"+num+++":"); 

   test.showStu(student); test.sop("");

 }

}

结果:

③通过姓名删除学生:
test.sop(test.stuDAO.delStuByName("李四"));

打印结果为2。


4.Junit的学习

稍后会发一篇博客。


明天做任务中与Junit单元测试有关的部分。


返回列表 返回列表
评论

    分享到