Testing a stored procedure using hsqldb

By Panos

Today I needed to test a data access object that will be calling a stored procedure in the database. Since we are using HSQLDB and mock objects to do all the database interactions while we develop, I needed to test it without having access to the actual stored procedure. At first I wasn’t sure how to do it. Then I started looking at the HSQLDB documentation and I realised that I could create an alias to a static method and treat it exactly like I could treat the stored procedure.

Lets say that the stored procedure returns a random number from 1 to 100. In my test class I define a static method that returns this number

public static int random()
{
return 1 + (int)(Math.random() * 100);
}

Then I create an alias to this method call


// Define the alias to the stored procedure as a
// static final variable
private static final String SP =
"CREATE ALIAS randomStoredProcedure
FOR\"my.class.DaoTester.random\"";

Note that I needed to define the full class name of my test class, including the package. I also used double quotes around the class declaration since HSQLDB automatically converts all lower case letters to upper case.

Once the stored procedure is loaded up in the in-memory database using

// Load the alias.
jdbc.update(SP);

it can be called from the actual DAO implementation class like you would call any other stored procedure


// Call it to get the random number.
int random = jdbc.queryForInt("{call randomStoredProcedure()}");

The jdbc variable is of type SimpleJdbcOperations from the Spring framework. The implementation class can be used without changes with the real stored procedure. Neat.

8 Responses to “Testing a stored procedure using hsqldb”

  1. music Says:

    very interesting.
    i’m adding in RSS Reader

  2. Panos Says:

    Thanks :)

  3. ilkinulas Says:

    Is there a way to return multiple values from stored procedure?

  4. Panos Says:

    It can return multiple values which you should read by using a ResultSet. If you want to simulate multiple values from a stored procedure you should make your test method return a ResultSet (or its implementation RowSet) which you will have filled up with the values needed.

  5. Larry Says:

    Good article.

    Can you describe more on how to fetch the return values, since normally I need to use “registerOutParameter()” function to do it.

    While from HSQLDB source codes (1.8.0.10) , it is stated it is not supported.

  6. Panos Says:

    Are you sure it’s not supported? This:

    http://hsqldb.org/doc/src/org/hsqldb/jdbc/jdbcCallableStatement.html#registerOutParameter(int,%20int)

    states that it is supported. It’s HSQLDB 1.7.2 that does not support this feature.

  7. Larry Says:

    I checked the lastest 1.8.0.10 source codes inside
    That’s the generic statement for the function, and probably they forget to update the statement for 1.8.0.10 as well

    public void registerOutParameter(int parameterIndex,
    int sqlType) throws SQLException {
    throw Util.notSupported();
    }

    I will post the question in help forum to get the result as well.

  8. Larry Says:

    It is stated it will be supported in 1.9.0.x release.

    This blog save me a lot of time, thx.

Leave a Reply