Mockrunner(Java) query with Regex

115 views Asked by At

I am using Mockrunner to mock Sql DB for my unit tests. Following is my query:-

"select * from table where userId in (" + userIds + ")"

Now my userIds is state dependent. I don't need my test cases dependent on the arrangement inside the list - userIds. So I don't need exact match but regex matching. I have already enabled regex matching by below code:-

    StatementResultSetHandler statementHandler = connection.getStatementResultSetHandler();
    usersResult = statementHandler.createResultSet("users");
    statementHandler.setUseRegularExpressions(true);
    //How to write this regex query?
    statementHandler.prepareResultSet("select * from table where userId in .*", campaignsResult); 

But as it is noted, I have no idea about the regex syntax supported by Mockrunner.

Edit: I unable to match queries like "Select * from tables" with "Select * from tab .*". So It has to do something with the way I using regex with Mockrunner

1

There are 1 answers

0
aroth On BEST ANSWER

There are some helpful examples available here. For instance:

public void testCorrectSQL() throws Exception {
    MockResultSet result = getStatementResultSetHandler().createResultSet();
    getStatementResultSetHandler().prepareResultSet("select.*isbn,.*quantity.*", result);
    List orderList = new ArrayList();
    orderList.add("1234567890");
    orderList.add("1111111111");
    Bookstore.order(getJDBCMockObjectFactory().getMockConnection(), orderList);
    verifySQLStatementExecuted("select.*isbn,.*quantity.*\\(isbn='1234567890'.*or.*isbn='1111111111'\\)");
}

From this, I surmise that it's using standard Java regex syntax. In which case, you probably want:

prepareResultSet("select \\* from table where userId in \\(.*\\)", campaignsResult);

...or perhaps more succinctly (and depending upon exactly how fine-grained your tests need to be):

prepareResultSet("select .* from table where userId in .*", campaignsResult);

The main caveat to be aware of when enabling the regex matching is that any literal special characters that you want in your query (such as *, (, and ) literals) need to be escaped in your regex before it will work properly.