单元测试之用户登录

发表于:2016-1-22 13:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:小号君    来源:51Testing软件测试网采编

  ### 小组:The Expendables
  测试代码:
/*UsersDAO代码*/
public class UsersDAO extends MyDAO{
//  private static JdbcUtil jdbcutil=new JdbcUtil();
public static boolean login(int type,String username,String password){
init();
String pwd=null;
try{
switch(type){
case 1:
sqlCommand="select * from 教 师  where 工号=?;";
break;
case 2:
sqlCommand="select * from 系负责人  where 工号=?;";
break;
case 3:
sqlCommand="select * from 教学办   where 工号=?;";
break;
}
conn=JdbcUtil.getConnection();
pst=conn.prepareStatement(sqlCommand);
//          pst.setString(1, tb_name);
pst.setString(1, username);
rs=pst.executeQuery();
while(rs.next()){
pwd=rs.getString("密码");
}
}catch(Exception e){
System.out.println(e.toString());
}
if(pwd==null||!pwd.equals(password)) return false;
else return true;
}
}
/*UsersDAOTest代码*/
/*测试用户登录,以教师用户为例(工号:21001,密码:21001)*/
public class UsersDAOTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testLogin() {
boolean result=UsersDAO.login(1, "21001", "21001");
if(result) System.out.println("ok!");
else System.out.println("not ok!!");
}
}
  测试结果:
  DB.properties works!
  com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'coursemanagement.教' doesn't exist
  not ok!!
  检查发现错误的地方:
  /*这行代码中的"教师"间多了一个空格*/
  sqlCommand="select * from 教 师  where 工号=?;";
  修改后的测试结果:
  DB.properties works!
  ok!
  分析结果:
  1、eclipse代码的字体需要放大,否则看不清楚中文间的空格。
  2、尽量避免在代码中使用中文,但是由于建立数据库时使用了中文,所以便“将错就错”进行开发,以后会避免出现此类低级错误。(ps:其实是因为英语太渣,才不用英文命名表名和列名,担心在修改数据库时会出错)
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号