Domanda

I am attempting to use Unitils to assist me in Database testing. I would like to use the Unitils/DBMaintain functionality for disabling constraints. However there is a few problems with this. I do not wish to use DBMaintain to create my databases for me however I wish to use its constraint disabling functionality. I was able to achieve this through the use of a custom module listed below:

public class DisableConstraintModule implements Module {

     private boolean disableConstraints = false;

     public void afterInit() {
         if (disableConstraints) {
             DatabaseUnitils.disableConstraints();
         }
     }

     public void init(Properties configuration) {
         disableConstraints = PropertyUtils.getBoolean("Database.disableConstraints", false, configuration);
     }      
}

This partially solves what I want however I wish to be able to only disable constraints for tables I will be using in my test. My tests will be running against a database with multiple schemas and each schema has hundreds of different tables. DatabaseUnitils.disableConstraints() disables the constraints for every table in every schema which would be far too time consuming and is unnecessary.

Upon searching the dbmaintain code I found that the Db2Database class does indeed contain a function for disabling constraints on a specific schema and table name basis however this method is protected. I could access this be either extending the Db2Database class or using reflection.

Next I need to be able to determine which schemas and tables I am interested in. I could do this by observing the @DataSet annotation to determine which schemas and tables are important based on what is in the xml. In order to do this I need to override the TestListener so I can instruct it to disable the constraints using the xml before it attempts to insert the dataset. This was my attempt at this:

public class DisableConstraintModule extends DbUnitModule {

    private boolean disableConstraints = false;

    private TableBasedConstraintsDisabler disabler;

    public void afterInit() {
    }

    public void init(Properties configuration) {
        disableConstraints = PropertyUtils.getBoolean("Database.disableConstraints", false, configuration);

        PropertyUtils.getInstance("org.unitils.dbmaintainer.structure.ConstraintsDisabler.implClassName", configuration);
    }

    public void disableConstraintsForDataSet(MultiSchemaDataSet dataSet) {
        disabler.disableConstraints(dataSet);
    }


    protected class DbUnitCustomListener extends DbUnitModule.DbUnitListener {

        @Override
        public void beforeTestSetUp(Object testObject, Method testMethod) {
            disableConstraintsForDataSet(getDataSet(testMethod, testObject));
            insertDataSet(testMethod, testObject);
        }
    }

 }

This is what I would like to do however I am unable to get the @DataSet annotation to trigger my DbUnitCustomListener and instead it calls the default DBUnitModule DbUnitListener. Is there anyway for me to override which listener gets called when using the @DataSet annotation or is there a better approach all together for disabling constraints on a specific schema and table level for a DB2 Database?

Thanks

È stato utile?

Soluzione

You have to tell Unitils to use your subclass of DbUnitModule. You do this using the unitils.module.dbunit.className property in your unitils.properties file. It sounds like you've got this part figured out.

The second part is to override DbUnitModule's getTestListener() in order to return your custom listener.

See this post for an example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top