- SoapUI Cookbook
- Rupert Anderson
- 578字
- 2021-07-23 20:19:19
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 calledinvoiceIdParam
with a value of2
. - Then, modify the SQL Query value to add a
where
clause to specify that the invoiceid
field must be equal to the value ofinvoiceIdParam
:select * from invoice where id=:invoiceIdParam
- Running the query should now return only the invoice number as
2
:<Results> <ResultSet fetchSize="0"> <Row rowNumber="1"> <INVOICE.ID>2</INVOICE.ID> <INVOICE.COMPANY>comp2</INVOICE.COMPANY> <INVOICE.AMOUNT>200</INVOICE.AMOUNT> <INVOICE.DUE_DATE>2014-12-01 00:00:00.0</INVOICE.DUE_DATE> </Row> </ResultSet> </Results>
- 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.
Tip
Property expansions
SoapUI has an expression language in order to reference properties across many of the objects in a project. For more info, see http://www.soapui.org/Scripting-Properties/property-expansion.html.
As an example:
- Add an
invoiceNo
property to theTestCase
that contains theJDBC Request TestStep
; that is, double-click on theTestCase
, click on the Properties tab, and add a new property with name asinvoiceNo
and value as1
. - Open the
JDBC Request TestStep
and edit theinvoiceIdParam
property value to contain:${#TestCase#invoiceNo}
- Run the
TestStep,
and you should see an invoice withid=1
!
- Add an
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:
INSERT INTO test.invoice (id, company, amount, due_date) VALUES (:invoiceIdParam, :invoiceCompanyParam, :invoiceAmountParam, :invoiceDueDateParam);
Tip
Property scopes
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.
See also
- For more information on property transfers, go to http://www.soapui.org/Functional-Testing/property-transfers.html
- Architecting the Industrial Internet
- Web Application Development with MEAN
- Java程序設(shè)計(jì)與實(shí)踐教程(第2版)
- Mastering Python Networking
- JavaScript悟道
- 數(shù)字媒體技術(shù)概論
- 讀故事學(xué)編程:Python王國歷險(xiǎn)記
- Processing開發(fā)實(shí)戰(zhàn)
- Python全棧開發(fā):數(shù)據(jù)分析
- Python GUI設(shè)計(jì)tkinter菜鳥編程(增強(qiáng)版)
- C#教程
- Java語言程序設(shè)計(jì)與實(shí)現(xiàn)(微課版)
- Building Web Applications with Flask
- Building Business Websites with Squarespace 7
- Unity 5.x 2D Game Development Blueprints