Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

DBUnit use case

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Concept

DbUnit is an extension of JUnit for database-driven projects. In addition to providing some common functions, it can put your database in a state between test cycles.

2. Maven Integration DBUnit2.1 Guide package org.dbunit dbunit 2.5.0 ch.qos.logback logback-access 1.1.3 ch.qos.logback logback-classic 1.1.3 mysql mysql-connector-java 5.1.37 2.2 create a data folder

1) create a dbunit folder. Store all the data information of dbunit

2) create a backup folder under the dbunit folder. Store database backup data files

3) create a test folder. Store all test data files

2.3Code collation 2.3.1 DBUnitBase base class

DBUnit description:

1) set database connection information

2) set the database data backup path

3) set the path of the test data file

4) data backup method and data recovery method

Package com.zzwx.test.dbunit.base; import java.io.File;import java.io.FileWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.List; import org.dbunit.database.DatabaseConnection;import org.dbunit.database.DatabaseDataSet;import org.dbunit.database.IDatabaseConnection;import org.dbunit.database.QueryDataSet;import org.dbunit.dataset.IDataSet;import org.dbunit.dataset.xml.FlatXmlDataSet;import org.dbunit.operation.DatabaseOperation / * * @ author Roger * @ desc DBUnit base class * / publicabstractclass DBUnitBase {/ * JDBC database connection * / protected Connection conn = null; / * DBUnit database connection * / protected IDatabaseConnection connection = null; / * backup data directory * / protected String backupDataPath = "src/test/resources/dbunit/backup/" / * Test data file directory * / protected String testDataPath = "src/test/resources/dbunit/test/"; / * data backup file * / protected File file = null; / * all backup files * / protected List files = null / * obtain database connection * * @ returnjava.sql.Connection * @ throws Exception * / protected Connection getConnection () throws Exception {Class.forName ("com.mysql.jdbc.Driver") / / Connect DB Connectionconn = DriverManager .getConnection ("jdbc:mysql://localhost:3306/zzwx?useUnicode=true&characterEncoding=UTF-8", "root", "123456"); returnconn } / * backup data through table name * * @ param tableName * table name * @ param backupFileName * backup file name * @ throws Exception * / protectedvoid backupData (String tableName, String backupFileName) throws Exception {try {conn = getConnection () Connection = new DatabaseConnection (conn); if (null! = tableName & &! ".equals (tableName)) {/ / back up single form data by table name QueryDataSetbackupDataSet = new QueryDataSet (connection); backupDataSet.addTable (tableName) / / set backup file path file = new File (backupDataPath + backupFileName); FlatXmlDataSet.write (backupDataSet, new FileWriter (file), "UTF-8") } else {/ / backup all data in the database IDataSetbackupDataSet = new DatabaseDataSet (connection, true); / / set the backup file path file = new File (backupDataPath + backupFileName) FlatXmlDataSet.write (backupDataSet, new FileWriter (file), "UTF-8");} catch (Exception e) {e.printStackTrace ();} finally {closeCon () }} / * recover data through xml file * * @ param fileName * path + file name * / @ SuppressWarnings ("deprecation") publicvoid recoverData (File file) {try {conn = getConnection (); connection = new DatabaseConnection (conn); IDataSetdataSet = new FlatXmlDataSet (file) DatabaseOperation.CLEAN_INSERT.execute (connection, dataSet);} catch (Exception e) {e.printStackTrace ();} finally {closeCon () }} / * close connection * / protectedvoid closeCon () {try {if (connection! = null) {connection.close ();} if (conn! = null) {conn.close () }} catch (SQLException e) {e.printStackTrace ();} 2.3.2 DBUnitAll

DBUnitAll description:

1) inherit the DBUnitBase base class

2) add test data and back up the pre-test data to src/test/resources/dbunit/backup/all_data_back.xml

3) method of backing up all data of database and method of restoring database data

4) when using this class, the setUpBackupAll (fileName) and recoverAllData () methods need to be called in pairs. Otherwise, the database data will not be restored to the data before the test.

Package com.zzwx.test.dbunit; import java.io.FileInputStream;import java.sql.Connection; 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 com.zzwx.test.dbunit.base.DBUnitBase / * * @ author Roger * @ desc back up all databases and all data in one backup file * / publicclass DBUnitAll extends DBUnitBase {/ * add test data And back up all the data of the database in the src/test/resources/dbunit/backup/all_data_back.xml * * @ param fileName * test data file * / @ SuppressWarnings ("deprecation") publicvoid setUpBackupAll (String fileName) {/ / JDBC database connection Connectionconn = null / / DBUnit database connection IDatabaseConnectionconnection = null; try {conn = getConnection (); / / obtain DB connection connection = new DatabaseConnection (conn); / / back up the data backupAll () before the database test / / IDataSetdataSet = new FlatXmlDataSet (new FileInputStream (testDataPath + fileName)); connection.createDataSet (new String [] {}); DatabaseOperation.CLEAN_INSERT.execute (connection, dataSet);} catch (Exception e) {e.printStackTrace () } finally {closeCon ();}} / * backup all data in the database * / publicvoid backupAll () {try {super.backupData (null, "all_data_back.xml");} catch (Exception e) {e.printStackTrace () }} / * restore database to pre-test * / publicvoid recoverAllData () {super.recoverData (file);}} 2.3.3 DBUnitEach

DBUnitEach description:

1) inherit the DBUnitBase base class

2) add test data, and back up the data that needs to be backed up data tables, backup data through the table name, each table for a backup data file

SetUpBackupEach () and recoverBackupEach () need to appear in pairs. Otherwise, the database data will not be restored to the data before the test.

Package com.zzwx.test.dbunit; import java.io.File;import java.io.FileInputStream;import java.sql.Connection;import java.util.ArrayList; 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 com.zzwx.test.dbunit.base.DBUnitBase / * * @ author Roger * @ desc add test data and back up the tables that need to be backed up Each table corresponds to a xml backup file * / publicclass DBUnitEach extends DBUnitBase {/ * add test data and backup data * * @ param fileName * file name * @ param tableNames * collection of table names to be backed up * / @ SuppressWarnings ("deprecation") publicvoid setUpBackupEach (String fileName, String...) TableNames) {/ / JDBC database connection Connectionconn = null; / / DBUnit database connection IDatabaseConnectionconnection = null; try {conn = getConnection (); / / get the DB connection connection = new DatabaseConnection (conn); / / back up the data backupDataEach (tableNames) before the database test / / IDataSetdataSet = new FlatXmlDataSet (new FileInputStream (testDataPath + fileName)); connection.createDataSet (new String [] {}); DatabaseOperation.CLEAN_INSERT.execute (connection, dataSet);} catch (Exception e) {e.printStackTrace () } finally {closeCon ();}} / * backup data by table name (one backup file per table) * * @ param tableNames * Table name collection * @ throws Exception * / publicvoid backupDataEach (String... TableNames) throws Exception {try {if (tableNames! = null & & tableNames.length > 0) {files = new ArrayList (); for (String tableName: tableNames) {super.backupData (tableName, tableName + "_ back.xml"); files.add (file) } catch (Exception e) {e.printStackTrace ();} finally {closeCon () }} / * restore backed up data * / publicvoid recoverBackupEach () {if (null! = files & & files.size () > 0) {for (File file: files) {super.recoverData (file);}} 2.3.4 DBUnitEachAll

DBUnitEachAll description:

1) inherit the DBUnitBase base class

2) add test data and back up all the data of the data table to be backed up to a backup file each_all_data_back.xml

3) the setUpBackupEachAll () and recoverBackupEachAll () methods appear in pairs. Otherwise, the database data will not be restored to the data before the test.

Package com.zzwx.test.dbunit; import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.sql.Connection; import org.dbunit.database.DatabaseConnection;import org.dbunit.database.IDatabaseConnection;import org.dbunit.database.QueryDataSet;import org.dbunit.dataset.IDataSet;import org.dbunit.dataset.xml.FlatXmlDataSet;import org.dbunit.operation.DatabaseOperation; import com.zzwx.test.dbunit.base.DBUnitBase / * @ author Roger * @ desc add test data and back up all data in the pre-test database to a backup file * / publicclass DBUnitEachAll extends DBUnitBase {/ * add test data and backup data * * @ param fileName * filename * @ param tableNames * table name collection * / @ SuppressWarnings ("deprecation") publicvoid setUpBackupEachAll (String fileName String... TableNames) {/ / JDBC database connection Connectionconn = null; / / DBUnit database connection IDatabaseConnectionconnection = null; try {conn = getConnection (); / / get the DB connection connection = new DatabaseConnection (conn); / / back up the data backupDataEachAll (tableNames) before the database test / / IDataSetdataSet = new FlatXmlDataSet (new FileInputStream (testDataPath + fileName)); connection.createDataSet (new String [] {}); DatabaseOperation.CLEAN_INSERT.execute (connection, dataSet);} catch (Exception e) {e.printStackTrace () } finally {closeCon ();}} / * back up the data in the database to a xml backup file by table name * * @ param tableNames * Table name collection * @ throws Exception * / publicvoid backupDataEachAll (String... TableNames) throws Exception {try {conn = getConnection (); / / get the DB connection connection = new DatabaseConnection (conn); QueryDataSetbackupDataSet = new QueryDataSet (connection) If (null! = tableNames & & tableNames.length > 0) {for (String tableName: tableNames) {backupDataSet.addTable (tableName);}} / / set the backup file path file = new File (backupDataPath + "each_all_data_back.xml") FlatXmlDataSet.write (backupDataSet, new FileWriter (file), "UTF-8");} catch (Exception e) {e.printStackTrace ();} finally {closeCon () }} / * restore backed up data * / publicvoid recoverBackupEachAll () {if (null! = file) {super.recoverData (file);}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report