加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

DBunit测试(实例)

发布时间:2020-12-12 15:23:38 所属栏目:MsSql教程 来源:网络整理
导读: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.DatabaseConnec

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读