简体   繁体   English

使用Regex进行Mockrunner(Java)查询

[英]Mockrunner(Java) query with Regex

I am using Mockrunner to mock Sql DB for my unit tests. 我正在使用Mockrunner来模拟Sql DB进行单元测试。 Following is my query:- 以下是我的查询:

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

Now my userIds is state dependent. 现在,我的userIds取决于状态。 I don't need my test cases dependent on the arrangement inside the list - userIds. 我不需要依赖于列表内部排列的测试用例-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. 但是请注意,我对Mockrunner支持的正则表达式语法一无所知。

Edit: I unable to match queries like "Select * from tables" with "Select * from tab .*" . 编辑:我无法将"Select * from tables""Select * from tab .*"匹配。 So It has to do something with the way I using regex with Mockrunner 所以它必须与我在Mockrunner中使用正则表达式的方式有关

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 . 由此,我推测它正在使用标准Java regex语法 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. 启用正则表达式匹配时要注意的主要警告是,您要在查询中使用的任何文字特殊字符(例如*()文字)必须先在正则表达式中转义,然后才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM