Java Reference
In-Depth Information
ensure that they are. Each time you run your unit-test suite, you should also run
these scripts to create the database schema. Using this approach, it should be easy
to commit a new set of database-creation scripts to your version control system,
and then run your unit tests to see if any changes caused problems for your appli-
cation. This is the ideal situation. If you are using an in-memory database such as
HSQLDB to run your tests, there might be an additional step to convert the sche-
mas. Consider automating this conversion as well to avoid the potential for
human error and speed up your integration time.
The iBATIS configuration file (i.e., SqlMapConfig.xml)
For the purposes of unit testing, you might want to use a separate i BATIS configu-
ration file. The configuration file controls your data source and transaction man-
ager configuration, which will likely vary greatly between the testing and
production environments. For example, in production you might be in a man-
aged environment such as a J2EE application server. In such an environment, a
managed DataSource instance is probably retrieved from JNDI . You may also be
leveraging global transactions in production. However, in your test environment
your application will probably not be running in a server; you'll have a simple
DataSource configured and will be using local transactions. The easiest way to con-
figure the test and production configurations independently is to have a different
i BATIS configuration file that references all of the same SQL mapping files as the
production one.
An iBATIS SqlMapClient unit test
Now that all of our prerequisites are ready, including a database instance, auto-
mated database build scripts, and a configuration file for testing, we're ready to
create a unit test. Listing 13.1 shows an example of using JU nit to create a simple
unit test.
Listing 13.1
Example of a SqlMapClient unit test
public class PersonMapTest extends TestCase {
private SqlMapClient sqlMapClient;
B
Setting up unit
test and test data
public void setup () {
sqlMapClient = SqlMapClientBuilder.
build("maps/TestSqlMapConfig.xml");
runSqlScript("scripts/drop-person-schema.sql");
runSqlScript("scripts/create-person-schema.sql");
runSqlScript("scripts/create-person-test-data.sql");
}
Search WWH ::




Custom Search