自己写的简单测试类

public class TestAddressDao extends DatabaseTestCase {
 
 private AddressDAO addDao=new AddressDAO();
 
 /**
  * 该方法用于获取连接
  */
 @Override
 protected IDatabaseConnection getConnection() throws Exception {
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBase=Test","sa","");
  return new DatabaseConnection(con);
 }

 /**
  * 该方法负责提取种子数据
  */
 @Override
 protected IDataSet getDataSet() throws Exception {
  System.out.println("URL:"+this.getClass().getResource("").getPath());
  return new FlatXmlDataSet(new FileInputStream(this.getClass().getResource("").getPath()+"testFileDB.xml"));
 }

 /**
  * 控制测试前的数据库状态
  */
 @Override
 protected DatabaseOperation getSetUpOperation() throws Exception {
  
  return DatabaseOperation.REFRESH;
 }

 /**
  * 控制测试后的数据库状态
  */
 @Override
 protected DatabaseOperation getTearDownOperation() throws Exception {
//  DatabaseOperation.DELETE;
//  DatabaseOperation.DELETE_ALL;
//  DatabaseOperation.INSERT;
//  DatabaseOperation.REFRESH;
//  DatabaseOperation.TRUNCATE_TABLE;
//  DatabaseOperation.UPDATE;
//  DatabaseOperation.NONE;
  return DatabaseOperation.DELETE;
 }
 
 public void testAddressDAO(){
  
  try {
   Address address=addDao.findById(3);
   TestCase.assertNotNull("河南",address.getAddName());
  } catch (Exception e) {
   System.out.println("测试出错!");
   e.printStackTrace();
  }
 }
 
 public void testDelectAllAddress(){
  try {
//   int delectAllAddress = addDao.DelectAllAddress();
//   TestCase.assertEquals(2,delectAllAddress);
  } catch (Exception e) {
   System.out.println("测试出错!");
   e.printStackTrace();
  }
 }
}

 

 

导出导入方法

 

 public void testExportRooms(File file,Connection connection) throws DatabaseUnitException,IOException, SQLException{
     IDatabaseConnection databaseConnection=new DatabaseConnection(connection);
     QueryDataSet dataSet=new QueryDataSet(databaseConnection);
     dataSet.addTable("room");
     Writer wirter=new FileWriter(file);
     XmlDataSetWriter w=new XmlDataSetWriter(wirter);
     w.write(dataSet);
     wirter.flush();
     wirter.close();
  }
 
 /**
  * 导入数据到ROOM表
  * 这个方法可以把上面生成的XML文件导入到数据库中,
  * 如果是其他格式的文件只需要更换IDataSetProducer的实现类就可以了
  * @param file  一个标准的java.io.File
  * @param connection 一个标准的java.sql.Connection
  */
 public void importRoom(File file, Connection connection) throws DatabaseUnitException, IOException, SQLException {
    // IDataSetProducer dataSetProducer = new XmlProducer(FileHelper.createInputSource(file));
//     IDataSet dataSet = new CachedDataSet(dataSetProducer);
//     IDatabaseConnection databaseConnection = new DatabaseConnection(connection);
//     DatabaseOperation operation = DatabaseOperation.CLEAN_INSERT;
//     DatabaseOperation.TRANSACTION(operation);
//     operation.execute(databaseConnection, dataSet);
//     DatabaseOperation.CLOSE_CONNECTION(operation);
 }
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值