Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8287967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:10:47+00:00 2026-06-08T12:10:47+00:00

I am attempting to use Unitils to assist me in Database testing. I would

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:10:48+00:00Added an answer on June 8, 2026 at 12:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use the following code to search a database using words
Attempting to use JRuby 1.2.0 and Rails 2.3.2 with an embedded Derby database. I've
I'm attempting to use the new Storyboard feature, however I am confused about Storyboard
Attempting to use a custom hex color for my css triangle (border). However since
I am attempting to use Castle Windsor in my automated tests like so: On
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I am attempting to use PHP to edit the DOM document tree. However, I
In attempting to use scipy's quad method to integrate a gaussian (lets say there's
I'm attempting to use both Perl's autoboxing functionality and operator overloading functionality, and they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.