Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对指定的值范围验证JSON响应中的多个参数_Json_Jmeter_Assert_Jsonresponse - Fatal编程技术网

针对指定的值范围验证JSON响应中的多个参数

针对指定的值范围验证JSON响应中的多个参数,json,jmeter,assert,jsonresponse,Json,Jmeter,Assert,Jsonresponse,使用JSON提取器,我成功地获取了以下数据片段,我需要从整个JSON响应中获取这些数据。现在,我需要验证每个字段的值范围,例如,年收入应该在1-10升/年之间,年龄18-30岁,教育程度可以是学士、理学学士。或者B.Com { : "ID":"M12345", : "EDUCATION":"B.A.", : "ANNUALINCOME":"5 - 6 L\/annum", : "AGE":"29" } mid_10= { : "ID":"M12346", : "EDUC

使用JSON提取器,我成功地获取了以下数据片段,我需要从整个JSON响应中获取这些数据。现在,我需要验证每个字段的值范围,例如,年收入应该在1-10升/年之间,年龄18-30岁,教育程度可以是学士、理学学士。或者B.Com

{
:   "ID":"M12345",
:   "EDUCATION":"B.A.",
:   "ANNUALINCOME":"5 - 6 L\/annum",
:   "AGE":"29"
}

mid_10=
{
:   "ID":"M12346",
:   "EDUCATION":"B.Sc.",
:   "ANNUALINCOME":"1 - 2 L\/annum",
:   "AGE":"24"
}

mid_11=
{
:   "ID":"M12347",
:   "EDUCATION":"B.Com.",
:   "ANNUALINCOME":"5 - 6 L\/annum",
:   "AGE":"27"
}
我应该在这里使用响应断言吗?我还需要检查哪些ID通过,哪些ID失败。理想情况下,我应该获得失败的ID,即使我没有获得通过的ID


提前感谢您的帮助。

您的接受标准太具体,因此您将无法使用响应断言

您将不得不使用以下示例断言代码:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

def income = response.ANNUALINCOME

def lowerBound = (income =~ "(\\d+)")[0][1] as int
def upperBound = (income =~ "(\\d+)")[1][1] as int
def average = (lowerBound + upperBound) / 2
def acceptableIncomeRange = 1..10
if (!acceptableIncomeRange.contains(average)) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage('Annual income is not within the acceptable range: ' + income)
}

def acceptableAgeRange = 18..30
def age = response.AGE as int

if (!acceptableAgeRange.contains(age)) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage('Age is not within the acceptable range: ' + age)
}

def acceptableEducations = ['B.A.', 'B.Sc.', 'B.Com']
def education = response.EDUCATION

if (!acceptableEducations.contains(education)) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage('Education is not within the acceptable range: ' + education)
}
参考资料:


感谢您的努力,Dmitri,但我收到断言失败消息:javax.script.ScriptException:javax.script.ScriptException:java.lang.IndexOutOfBoundsException:index超出范围0..-1(index=0)很可能正则表达式与
ANNUALINCOME
值不匹配,除非看到有效的JSON,不是你说的那些废话