如何在JMeter中提取数据库Diagnosis Reference字段指定位置的值并与JSON变量对比
Got it, let's walk through how to pull off this task in JMeter step by step. I'll break it down into clear, actionable steps that you can follow directly:
First, you'll need to use a JDBC Request Sampler to fetch the value from your database. Make sure you've already set up a JDBC Connection Configuration to connect to your database.
- In the JDBC Request Sampler, enter your query (something like
SELECT DiagnosisReference FROM your_table WHERE your_condition;). - In the Variable names field, input a variable name like
diagnosisRef— this will store the result. For a single-row result, the value will be available as${diagnosisRef_1}.
Next, we'll split that 6-digit string into separate variables for each position. The most flexible way to do this is with a JSR223 Sampler (use Groovy for best performance):
Add a JSR223 Sampler, paste this code:
// Grab the DB value we extracted earlier def diagnosisRef = vars.get("diagnosisRef_1") // First, make sure it's exactly 6 digits (optional but recommended) if (diagnosisRef?.length() != 6) { log.error("Diagnosis Reference is not 6 digits: ${diagnosisRef}") AssertionResult.setFailure(true) AssertionResult.setFailureMessage("Diagnosis Reference must be 6 characters long") return } // Split each digit and store as separate variables for (int i = 0; i < diagnosisRef.length(); i++) { def digit = diagnosisRef.charAt(i).toString() vars.put("DB_Diagnosis_Ref_${i+1}", digit) }
This will create variables like ${DB_Diagnosis_Ref_1} (first digit), ${DB_Diagnosis_Ref_2} (second digit), all the way up to ${DB_Diagnosis_Ref_6}.
Now, extract the corresponding Diagnosis Reference X values from your JSON response using a JSON Extractor:
- Add a JSON Extractor as a child to the HTTP Request that returns your JSON.
- Set Reference Name to
JSON_Diagnosis_Ref. - In the JSON Path Expressions section, add these 6 entries (one per position):
$.['Diagnosis Reference 1']$.['Diagnosis Reference 2']$.['Diagnosis Reference 3']$.['Diagnosis Reference 4']$.['Diagnosis Reference 5']$.['Diagnosis Reference 6']
- Set Match No. to
1(since we want the first match for each).
This will generate variables like ${JSON_Diagnosis_Ref_1} to ${JSON_Diagnosis_Ref_6} for each JSON value.
Finally, we'll verify that each DB digit matches the corresponding JSON value. Use a JSR223 Assertion for this (again, Groovy is best):
Add a JSR223 Assertion, paste this code:
boolean allMatches = true // Loop through each position and compare for (int i = 1; i <= 6; i++) { def dbVal = vars.get("DB_Diagnosis_Ref_${i}") def jsonVal = vars.get("JSON_Diagnosis_Ref_${i}") if (dbVal != jsonVal) { log.error("Mismatch at position ${i}: DB value = ${dbVal}, JSON value = ${jsonVal}") AssertionResult.setFailure(true) AssertionResult.setFailureMessage("Diagnosis Reference ${i} mismatch: expected ${dbVal}, got ${jsonVal}") allMatches = false // Uncomment below if you want to stop checking after the first mismatch // break } } if (allMatches) { log.info("All Diagnosis Reference digits match between DB and JSON!") }
This will flag a failure if any digit doesn't match, and log the details of the mismatch. You can choose to stop at the first error or check all positions by adjusting the break line.
内容的提问来源于stack exchange,提问作者Harsha

