单元测试之查询接口

小组:The Expendables

查询接口主要是实现大量数据的查询,将查询得到的数据以list形式封装成json数据并发送给客户端。

由于其它表中没有数据,所以以教师信息表为例进行测试。

测试代码:

/*GeneralDAO代码*/
public static List<Tb_teacher> queryTeacher(String table_name){
        init();
        List<Tb_teacher> tb_teacher=new ArrayList<Tb_teacher>();
        try{
            
            sqlCommand="select * from "+table_name;
            conn=JdbcUtil.getConnection();
            pst=conn.prepareStatement(sqlCommand);
//          pst.setString(1, tb_name);
            rs=pst.executeQuery();
            while(rs.next()){
                Tb_teacher tb=new Tb_teacher();
                
                tb.setId(rs.getString("工号"));
                tb.setPassword(rs.getString("密码"));
                tb.setDepartment(rs.getString("所属系"));
                tb.setName(rs.getString("姓名"));
                tb.setSex(rs.getString("性别"));
                tb.setBirth(rs.getString("出生年月"));
                tb.setEmail(rs.getString("邮箱"));
                tb.setPhone(rs.getString("手机号码"));
                
                tb_teacher.add(tb);
            }
            
            pst.close();
            conn.close();
        }catch(Exception e){
            System.out.println(e.toString());
        }
        return tb_teacher;
    }
/*GeneralDAOTest代码*/
public class GeneralDAOTest {
    @Test
    public void testQueryTeacher() {
        String table_name = "教师信息表";
        List<Tb_teacher> tb_teacher = GeneralDAO.queryTeacher(table_name);
        for (int i = 0; i < tb_teacher.size(); i++) {
            System.out.println(tb_teacher.get(i));
        }
    }
}

测试结果:

DB.properties works!
com.tb.Tb_teacher@7d4793a8
com.tb.Tb_teacher@449b2d27
com.tb.Tb_teacher@5479e3f
com.tb.Tb_teacher@27082746
com.tb.Tb_teacher@66133adc

你可能感兴趣的:(单元测试之查询接口)