ITeye博客

dbunit进行数据库操作的测试

kabike · 2013-02-21 21:26 · tools · 行业应用 · dbunit · Markdown

j2ee程序好多都涉及到了数据库操作,这时进行mock测试就没有意义,不如直接在数据库上进行操作. 进行数据库测试有两点要求 1 测试之前数据库要处在一个确定性的状态下. 2 测试不改变数据库的状态. 我觉得比较好的方法是建立一个专用的测试数据库,避免了测试数据的干扰.否则每次测试结束就要进行相应的现场恢复. 假设我们有个开发数据库allcrap,然后建立个测试数据库allcrap_test.为了保证数据库表结构的一致,可以人工同步,也可以用mysqldump进行 .\mysqldump.exe -u root -proot allcrap -d > ddl.sql 这个命令导出了allcrap数据库DDL语句,-d选项是表示不用导出数据,然后把DDL语句导入测试库即可. .\mysql -u root -proot allcrap_test [/code] 这样dbunit就能在每个测试之前把数据库的product表初始化为这三条数据. 随后的每一个测试是建立在确定性的状态上,也就能够用程序来进行自动化测试,而不用人工干预. 这样每天都可以用定时任务进行测试. 一个dbunit的testcase如下 [code="java"] public class ProductDaoTest extends DBTestCase { public ProductDaoTest() { System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver"); System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:mysql://localhost/allcrap_test"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "root"); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "root"); } @Override protected IDataSet getDataSet() throws Exception { return new FlatXmlDataSetBuilder().build(new FileInputStream( ".\\bin.\\product_dataset.xml")); } private ProductDao productDao; private SessionFactory sessionFactory; private Session session; @Override protected void setUp() throws Exception { super.setUp(); sessionFactory = new Configuration() .configure("hibernate_test.cfg.xml").buildSessionFactory(); this.session = sessionFactory.openSession(); productDao = new ProductDao(); productDao.setSession(session); } @Override protected void tearDown() throws Exception { if (this.session != null) { this.session.close(); } if (this.sessionFactory != null) { this.sessionFactory.close(); } super.tearDown(); } private void beginTx() { this.session.beginTransaction(); } private void commit() { this.session.getTransaction().commit(); } @Test public void testBasicCRUD() { this.beginTx(); Product p = this.productDao.load(2); assertEquals(Integer.valueOf(2), p.getId()); Product newProduct = new Product(); newProduct.setName("testProduct"); newProduct.setPrice(BigDecimal.valueOf(34.2)); this.productDao.save(newProduct); this.commit(); try { assertEquals("testProduct", this.getConnection() .createDataSet().getTable("product").getValue(3, "name")); } catch (Exception e) { e.printStackTrace(); } } @Test public void testProductListByPrice() { this.beginTx(); List productList = this.productDao.findProductListByPrice( new BigDecimal(400), new BigDecimal(600)); assertEquals(2, productList.size()); assertEquals(Integer.valueOf(2), productList.get(0).getId()); assertEquals(Integer.valueOf(3), productList.get(1).getId()); this.commit(); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。