微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DBunit测试实例

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.FileHandler;

import junit.framework.TestCase;

import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaSEOperation;

import  .......dao.AddressDAO;
import .......entity.Address;

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();  } }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐