DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, , The java API documentation is now available online. DbUnit is a JUnit extension (also usable with Ant) targeted for database-driven projects that, among other things, puts your database into a known state between . Spring DBUnit provides integration between the Spring testing framework and the popular DBUnit project. It allows you to setup and teardown database tables.
|Published (Last):||2 June 2012|
|PDF File Size:||17.64 Mb|
|ePub File Size:||2.83 Mb|
|Price:||Free* [*Free Regsitration Required]|
Your test need some data to work with.
This means you must create a dataset. In most situations you will work with xml datasets. You can manually create a flat XML dataset from scratch or create one by exporting some data from your database. Now you need to create a test class.
A documentaation method is required to be implemented: The simplest way to configure dubnit would be in the constructor of your test class. You may modify this behavior by overriding getDatabaseTesterusing one of the other 3 provided IDatabaseTester implementations or your own. The following is a sample implementation that returns a connection to a Hypersonic database and a xml dataset:. The following example demonstrates how you can easily override the operation executed before or after your test.
Implement your test methods as you normally would with JUnit. Your database is now initialized before and cleaned-up after each test methods according to what you did in previous steps.
You can override the standard JUnit setUp method and execute the desired operation on your database. Do something similar in teardown if you need to perform clean-up.
Currently there documentatioj 4 convenient implementations: In order to use Dbunit you are not required to extend any classes. Dbunnit configure an instance of a DBTestCase subclass, whether directly instantiated or dependency injected documemtation test classes. Dbunit provides support for verifying whether two tables or datasets contain identical data. The Assertion class has many methods for verifying if your database contains the expected data during test cases execution.
Here are some of them:. The following sample shows how to compare a database table snapshot against a flat XML table using equality comparison.
The actual dataset is a database snapshot you want to verify against an expected dataset.
Welcome to the DbUnit Wiki! – DbUnit
As its name imply, the expected dataset contains the expectation values. The expected dpcumentation must be different from the one you have used to setup your database. Therefore you need two datasets to do that; one to setup your database before a test and another to provide the expected data during the test. You can also verify if the result of a query match an expected set of data. The query can be used to select only a socumentation of a table or even join multiple tables together.
Sometimes this is desirable to ignore some columns to perform the comparison; particularly for primary keys, date or time columns having values generated by the code under test.
One way to do this is to omit to declare unwanted columns in your expected table. You can then filter the actual database table to only expose the expected table columns. The following code snippet shows you how to filter the actual table. Extra columns can exist in the actual table but not in the expected one. With a DTD you need to filter columns from both the expected and the actual table. See the FAQ about excluding some table columns at runtime.
By default, database table snapshot taken by DbUnit are dunit by primary keys. If a table does not have a primary key or the primary key is automatically generated by your database, the rows ordering is not predictable and assertEquals will fail. You must order your database snapshot manually by using IDatabaseConnection. Or you can use the SortedTable decorator class like this:. By default, dbunit immediately fails when the first data difference was found.
Starting with dbunit 2. Using the DiffCollectingFailureHandler you can avoid an exception to be thrown on a data mismatch so that you can evaluate all results of the data comparison afterwards.
Nearly all tests need to load data from one or more files, particularly for prep or expected data. The available loaders are in package org. A simple usage example:. Note the constructors for the various DataFileLoaders accept replacement object and replacement substring maps as used with ReplacementDataSet. WebTest is a tool to simulate a user’s browser clicking through the pages on a web site.
It allows you to create a series of Ant based tests for your website. This document walks you through a suggested format for storing tests. Your first step is to create your dataset file that you want to load into your database before running your WebTest script.
Use one of the various methods described above. A suggested setup is to have a single build.
This would include a couple targets like:. Once you have your build.
Database unit testing with DBUnit, Spring and TestNG
This way you can either run all the test’s in your Test Suite, or just run a specific one, all from build. Now you need to write your dockmentation testcases. For more information on WebTest, please refer to the WebTest home page.
If you have a single set of properties, then load them as part of build. If you have multiple databases you need to connect to, then declare your sql connection properties in a TestSuiteXXX. Quick Links Maven 1. Create documentatikn dataset file Your test need some data to work with. You may also use a subclass of DBTestCase, such as one of these: DefaultPrepAndExpectedTestCase uses a configurable IDatabaseTester allowing any connection type providing a turn-key test setup and verification process in one, with clear separation of prep and expected datasets.
Refer to dbUnit Test Cases page for more docunentation. The following is documentaion sample implementation that returns a connection to a Hypersonic database and a xml dataset: The following example demonstrates how you can easily override this method: Implement your testXxx methods Implement your test methods as you normally would with JUnit.
Database setup with no parent class In order to use Dbunit you are not required to extend any classes. Database data verification Dbunit provides support for verifying whether two tables or datasets contain identical data. Here are some of them: Sample The following sample shows how to compare a database table snapshot against documenttaion flat XML table using equality comparison.
Using a query to take the database snapshot You can also verify if the result of a query match an expected set of data.
dbUnit Extension SNAPSHOT API
Ignoring some columns in comparison Sometimes this is desirable to ignore some columns to perform the comparison; particularly for primary keys, date or time columns having values generated by the code under test. Row ordering By default, database documentatjon snapshot taken by DbUnit are sorted by primary keys. Or you can use dgunit SortedTable decorator class like this: Assert and collect the differences By default, dbuit immediately fails when the first data difference was found.
Data File Loader Nearly all tests need to load data from one or more files, particularly for prep or expected data. A simple usage example: Create your dataset file Your first step is to create your dataset file that you want to load into your database socumentation running your WebTest script.
Create your Ant build. This would include a couple targets like: Runs all the testSuites that you have created test: Runs a single test in a specific testSuite test: Runs all the tests for a specific testSuite. Create your various Test Suites Once you have your build.
Create your various Tests Now you need to write your various testcases. Sample Directory Layout When you are done, you will have a series of files that look like this: This is the default implementation used by DBTestCase.