Parameterizing SQL queries with the JDBC Request TestStep
This recipe builds directly on the last one to show how the JDBC Request TestStep can be used to execute parameterized SQL queries based on property values from outside the TestStep. This can be useful, as it allows the JDBC Request TestStep to query and check data based on properties set from the results of other TestSteps, for example, executing a query using an ID obtained from a web service response.
How to do it...
First, we add a new parameter with a fixed value to the JDBC Request TestStep and use it as the criteria for a simple select query. Then, we change the JDBC Request TestStep parameter to take its value from the value of a TestCase property. Perform the following steps:
Edit the JDBC Request TestStep from the previous recipe and add a new parameter called invoiceIdParam with a value of 2.
Then, modify the SQL Query value to add a where clause to specify that the invoice id field must be equal to the value of invoiceIdParam:
select * from invoice where id=:invoiceIdParam
Note
Placeholder syntax
Use : before the intended parameter name in the query.
Running the query should now return only the invoice number as 2:
So it works, but big deal! To make this more useful, we can try and use a SoapUI property expansion to get the parameter value from somewhere outside of the test step.
Add an invoiceNo property to the TestCase that contains the JDBC Request TestStep; that is, double-click on the TestCase, click on the Properties tab, and add a new property with name as invoiceNo and value as 1.
Open the JDBC Request TestStep and edit the invoiceIdParam property value to contain:
${#TestCase#invoiceNo}
Run the TestStep, and you should see an invoice with id=1!
How it works...
This recipe has been mostly explained as we did it. The key learnings are the syntax used for query parameters in a JDBC Request TestStep and the property expansion expression language. Property expansions are a very important concept in SoapUI, as they effectively allow data to be passed between related objects like TestSteps. They can be used in many other places to insert property values. Common examples would be setting the value of variables in a Groovy script or setting properties in a web service request.
There's more...
Another example would be to use property expansions with the JDBC Request TestStep to insert data gathered by a previous step, for example, to store test results in a database for an external reporting tool to use or to populate a mock service's test data. To insert data based on parameter values, you could use a query like the following one:
When using property expansions, it can be important to consider the property's scope, especially if you update them. For example, a project or a globally scoped property that is updated by multiple TestCases could lead to concurrency or thread-safety issues. In general, try to keep the scope as narrow as possible for writeable properties and as broad as possible for read-only properties.