Getting started with dbunit

Source: Internet
Author: User

Dbunit is a Java unit test framework extended based on JUnit.

If your business logic involves adding, deleting, modifying, and querying records in the database, and you do not want to manually query records in the database every time to verify your business logic, so dbunit can help you. In fact, if your unit test only has a small amount of business logic to operate on the database, then the advantages of dbunit in unit test cannot be reflected. However, if your unit test case contains a large number of DaO operations, you can manually execute database queries, which may reduce your work efficiency and may omit functions. In addition, in regression testing, the work of verifying DaO operations cannot be reused.

Now let's get to know dbunit! First, we will introduce the org. dbunit. databasetestcase. Java class. The source code is as follows:

Package org. dbunit;

Import JUnit. Framework. testcase;
Import org. dbunit. database. idatabaseconnection;
Import org. dbunit. dataset. idataset;
Import org. dbunit. Operation. databaseoperation;

/***//**
* @ Author Manuel Laflamme
* @ Version $ revision: 1.11 $
* @ Since Feb 17,200 2
*/
Public abstract class databasetestcase extends testcase
...{
Public databasetestcase ()
...{
}

Public databasetestcase (string name)
...{
Super (name );
}

/***//**
* Returns the test database connection.
*/
Protected abstract idatabaseconnection getconnection () throws exception;

/***//**
* Returns the test dataset.
*/
Protected abstract idataset getdataset () throws exception;

/***//**
* Close the specified connection. ovverride this method of you want
* Keep your connection alive between tests.
*/
Protected void closeconnection (idatabaseconnection connection) throws exception
...{
Connection. Close ();
}

/***//**
* Returns the database operation executed in test setup.
*/
Protected databaseoperation getsetupoperation () throws exception
...{
Return databaseoperation. clean_insert;
}

/***//**
* Returns the database operation executed in test cleanup.
*/
Protected databaseoperation getteardownoperation () throws exception
...{
Return databaseoperation. None;
}

Private void executeoperation (databaseoperation operation) throws exception
...{
If (operation! = Databaseoperation. None)
...{
Idatabaseconnection connection = getconnection ();
Try
...{
Operation.exe cute (connection, getdataset ());
}
Finally
...{
Closeconnection (connection );
}
}
}


//////////////////////////////////////// ////////////////////////////////////
// Testcase class

Protected void setup () throws exception
...{
Super. Setup ();

Executeoperation (getsetupoperation ());
}

Protected void teardown () throws exception
...{
Super. teardown ();

Executeoperation (getteardownoperation ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.