Thursday, June 11, 2009

DBUnit & MySQL: DataTypeFactory warning

I've been playing with DBUnit to help setup a database for my integration testing, and the experience has been very good. When my dataset was being loaded into my MySQL database, I found this warning:
    [WARN ] [] - Potential problem found: 
The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory'
might cause problems with the current database 'MySQL' (e.g. some datatypes may not be
supported properly).
In rare cases you might see this message because the list of supported database products is
incomplete (list=[derby]). If so please request a java-class update via the forums.
I found the following DBUnit FAQ Entry on the DBUnit website that helped me out a bunch, the code from the entry:
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection, schema);
DatabaseConfig config = connection.getConfig();
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new OracleDataTypeFactory());
My code additions:
def dbConfig = dbConnection.getConfig();
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory()
And all my Groovy DBUnit related code:
import org.dbunit.database.DatabaseConfig
import org.dbunit.database.DatabaseConnection
import org.dbunit.dataset.xml.XmlDataSet
import org.dbunit.ext.mysql.MySqlDataTypeFactory
import org.dbunit.operation.DatabaseOperation

class MyIntegrationTestCase extends GroovyTestCase {


def setUpDatabase() {
def connection = getConnection()
def data = getDataSet()

try {
DatabaseOperation.CLEAN_INSERT.execute(connection, data)
finally {

def getConnection() {
def jdbcConnection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
def dbConnection = new DatabaseConnection(jdbcConnection)

def dbConfig = dbConnection.getConfig();
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory())

return dbConnection

def getDataSet() throws Exception {
def stream = getClass().getClassLoader().getResourceAsStream(DATASET)
def dataset = new XmlDataSet(stream)
return dataset


No comments:

Post a Comment