It is never a good idea to hard code database login details in code. For this reason, Testaco uses a configuration file that describes the databases that exist around an application.
This file should be checked in to your source control repository together with your source code. This will make it much easier for other developers to pick up your code and get the tests running. In order to do this, it will also be necessary to standardize on a standard login and some standard schema names that are used throughout your organization. Since Testaco will never load the database with anything else than test data, it is not a very large security risk if all machines that are able to run your tests share this data - you will never have production data in these databases.
<?xml version="1.0" encoding="UTF-8"?>
<appConfig:applicationConfiguration xmlns:appConfig="http://www.example.org/ApplicationConfiguration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in
stance" xsi:schemaLocation="http://www.example.org/ApplicationConfiguration-1.0 ApplicationConfiguration.xsd ">
<appConfig:localDatabaseDefinition name="default">
<appConfig:driverClass>com.mysql.jdbc.Driver</appConfig:driverClass>
<appConfig:connectionURI>jdbc:mysql://localhost/dbtester?characterEncoding=utf8&allowMultiQueries=true</appConfig:connectionURI>
<appConfig:loginName>root</appConfig:loginName>
<appConfig:loginPassword></appConfig:loginPassword>
<appConfig:emptyDbUnitFileLocation>tests-data/empty.xml</appConfig:emptyDbUnitFileLocation>
<appConfig:databaseType>mysql</appConfig:databaseType>
<appConfig:schemaName>dbtester</appConfig:schemaName>
<appConfig:tableModification name="customer">
<appConfig:columnModification name="lastModified">ignore</appConfig:columnModification>
</appConfig:tableModification>
</appConfig:localDatabaseDefinition>
</appConfig:applicationConfiguration>
In this xml file, the connection details for a MySQL database schema called "dbtester" is defined. Please note the connection properties - the allowMultiQueries attribute is necessary in the URI since MySQL has a piece of functionality (It is very hard to describe this as a "feature") where a JDBC statement can only contain one SQL query. It is necessary to turn this feature off when using testaco as testaco may try to recreate the database from a DDL file - which is probably going to contain several statements.
Your application is problably going to try to contact the database using a JDBC driver. If this is the case, Testaco will find this JDBC driver on the application class path. If not, a suitable driver will have to be made available on the class path.
The database definition contains a file that contains an "empty" database definition. This file is defined using the "emptyDbUnitFileLocation" element. This file is used for refactoring tooling support.
Testaco also has support for ignoring columns in a table - for instance, columns containing datetimes can be notoriously hard to test. This is an area where dbunit could improve. In the meantime, the tableModification and columnModification elements allow ignoring of tables on a per-table basis. In the above example, the "lastModified" column in the "customer" table will be ignored in all Testaco data sets - you will have to test the behaviour of this column on your own.
If your application has several databases, these can be defined using multiple localDatabaseDefinition elements with different name attributes:
(...)
<appConfig:localDatabaseDefinition name="default">
(...)
<appConfig:localDatabaseDefinition name="extradatabase">
(...)
These database definitions are then made available to Testaco, and can be used in tests:
@LoadDb(datafile = "tests-data/default-empty-set.xml")
@LoadDb(database = "extradatabase", datafile = "tests-data/extradatabase-empty-set.xml")
@CheckDb(datafile = "tests-data/singleentry.xml")
(...)
public void testIt() throws Exception {
(...)
}
}