DbUnit开发实例
下面通过一个实例来说明DbUnit的实际运用。
实例准备
比如有一个学生表[student],结构如下:
然后我们说明如何对这2个方法进行单元测试。
实例展开
1 把准备数据和预想数据转换成xml文件
student_pre.xml
<?xml version='1.0' encoding="gb2312"?> <dataset> <student id="0001" name="翁仔" sex="m" birthday="1979-12-31"/> <student id="0002" name="王翠花" sex="f" birthday="1982-08-09"/> </dataset>
<?xml version='1.0' encoding="gb2312"?> <dataset> <student id="0001" name="翁仔" sex="m" birthday="1979-12-31"/> <student id="0002" name="王翠花" sex="f" birthday="1982-08-09"/> <student id="0088" name="王耳朵" sex="m" birthday="1982-01-01"/> </dataset>
2 实装setUp方法,详细见代码注释。
protected void setUp() { IDatabaseConnection connection =null; try{ super.setUp(); //本例使用postgresql数据库 Class.forName("org.postgresql.Driver"); //连接DB Connection conn=DriverManager.getConnection("jdbc:postgresql:testdb.test","postgres","postgres"); //获得DB连接 connection =new DatabaseConnection(conn); //对数据库中的操作对象表student进行备份 QueryDataSet backupDataSet = new QueryDataSet(connection); backupDataSet.addTable("student"); file=File.createTempFile("student_back",".xml");//备份文件 FlatXmlDataSet.write(backupDataSet,new FileOutputStream(file)); //准备数据的读入 IDataSet dataSet = new FlatXmlDataSet( new FileInputStream("student_pre.xml")); DatabaseOperation.CLEAN_INSERT.execute(connection,dataSet); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(connection!=null) connection.close(); }catch(SQLException e){} } }
// findStudent public void testFindStudent() throws Exception{ //执行findStudent方法 StudentOpe studentOpe=new StudentOpe(); Student result = studentOpe.findStudent("0001"); //预想结果和实际结果的比较 assertEquals("翁仔",result.getName()); assertEquals("m",result.getSex()); assertEquals("1979-12-31",result.getBirthDay()); }
public void testAddStudent() throws Exception{ //执行addStudent方法 StudentOpe studentOpe=new StudentOpe(); //被追加的记录 Student newStudent = new Student("0088","王耳朵","m","1982-01-01"); //执行追加方法 Student result = studentOpe.addStudent(newStudent); //预想结果和实际结果的比较 IDatabaseConnection connection=null; try{ //预期结果取得 IDataSet expectedDataSet = new FlatXmlDataSet(new FileInputStream("student_exp.xml")); ITable expectedTable = expectedDataSet.getTable("student"); //实际结果取得 Connection conn=getConnection(); connection =new DatabaseConnection(conn); IDataSet databaseDataSet = connection.createDataSet(); ITable actualTable = databaseDataSet.getTable("student"); //比较 Assertion.assertEquals(expectedTable, actualTable); }finally{ if(connection!=null) connection.close(); } }
ITable filteredExpectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"birthday"}); ITable filteredActualTable = DefaultColumnFilter.excludedColumnsTable(actualTable,new String[]{"birthday"}); Assertion.assertEquals(filteredExpectedTable, filteredActualTable);
protected void tearDown() throws Exception{ IDatabaseConnection connection =null; try{ super.tearDown(); Connection conn=getConnection(); connection =new DatabaseConnection(conn); IDataSet dataSet = new FlatXmlDataSet(file); DatabaseOperation.CLEAN_INSERT.execute(connection,dataSet); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(connection!=null) connection.close(); }catch(SQLException e){} } }