DBUnit: how to change a property value correctly?

荒凉一梦 提交于 2019-12-10 11:23:59

问题


I have the problem with DBUnit (V2.5.3) that I want to set the property FEATURE_ALLOW_EMPTY_FIELDS to true but DBUnit ignores this setting. My code to set the property is:

DatabaseConfig dbCfg = null;
try {
  dbCfg = dbTester.getConnection().getConfig();
  dbCfg.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);

  System.out.println("getCfg -> " + dbTester.getConnection().getConfig().getProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS));
  System.out.println("dbCfg  -> " + dbCfg.getProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS));

} catch (Exception exc) {
  exc.printStackTrace();
}

I think the problem is that the new value is only set to the dbCfg object. But it seems that DBUnit don't use this object becaus the output of the code above is:

getCfg -> false
dbCfg  -> true

Looks as if I am too stupid to understand how to set a DBUnit property correctly ...


回答1:


The problem is that IDatabaseTester.getConnection() seems to return with each call a new IDatabaseConnection object. I thought that this returns each time the same connection object.

EDIT

@JavaDev1987: Memorize the object returned by IDatabaseTester.getConnection(). At example use following code instead the code which I have post in my question:

IDatebaseConnection dbConn = null;
DatabaseConfig dbCfg = null;
try {
  dbConn = dbTester.getConnection();  // <-- get DBConnection
  dbCfg = dbConn.getConfig();    // use dbConn instead calling 'dbTester.getConnection()'
  dbCfg.setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, Boolean.TRUE);

  // in next line: use here also dbConn instead calling 'dbTester.getConnection()'
  System.out.println("getCfg -> " + dbConn.getConfig().getProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS));
  System.out.println("dbCfg  -> " +    dbCfg.getProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS));

} catch (Exception exc) {
  exc.printStackTrace();
}

Important is that you don't call dbTester.getConnection() again, because this will create and return a NEW database connection (with a new and unchanged default configuration). That's what I wrote.




回答2:


I noticed there is an OperationListener in there, which is triggered after the Connection object is generated. Here's what I've done: I added a CustomConfigurationOperationListener:

public class CustomConfigurationOperationListener extends DefaultOperationListener implements IOperationListener{
    @Override
    public void connectionRetrieved(IDatabaseConnection iDatabaseConnection) {
        super.connectionRetrieved(iDatabaseConnection);
        iDatabaseConnection.getConfig().setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true);
    }
}

And then setted it on the dbTester:

dbTester.setOperationListener(new CustomConfigurationOperationListener());

That way we override the configuration after the connection is generated, and before the dataset is created.



来源:https://stackoverflow.com/questions/40629855/dbunit-how-to-change-a-property-value-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!