Extending SQLUnit: Adding your own Assertion

Assertions can be of two kinds, one specific to the batchtest tag (involving BatchDatabaseResult objects) and the other specific to diff and test tags (involving DatabaseResult objects). The following things need to be done to add a new assertion.

  1. Determine if the tag is for the batchtest tag or the diff/test tags.

  2. Add an entry for the mapping from the assert string to the actual method name to invoke.

  3. Implement the method.

This can be most easily explained by means of an example. Suppose we wanted to implement an assertion "in-same-ballpark" for DatabaseResult objects. We wish to map this assertion to the method assertInSameBallpark(). The mapping to set up is shown below.


    private static final String[] DATABASE_RESULT_ASSERT_MAPPING = {
        {"equal", "assertEqual"},
        // more mappings here
        {"in-same-ballpark", "assertInSameBallpark"}
    };
      

We then implement the logic to determine if two DatabaseResult objects are in the same ballpark. This involves adding a method and implementing it, like so:


    private static void assertInSameBallpark(String failureMessage,
            DatabaseResult expR, DatabaseResult gotR, List matchPatterns)
            throws SQLUnitException {
        // some logic here. If the test fails it should throw an
        // SQLUnitException with an ASSERT_FAILED message.
    }
      

Look at the code for Assertions.java for more information.