我已经编写了代码来断言从数据库存储的值。
//SQL statement
String dbQuery2 = /SELECT * FROM public.test where testId = 'default'/
//Connect to PostgresSQL, global variable is stored at profile
List results = CustomKeywords.'test.database.getPostgresSQLResults'(GlobalVariable.dbConnString2 , GlobalVariable.dbUsername2 , GlobalVariable.dbPassword2 ,GlobalVariable.dbDriver2 ,dbQuery2 )
println(results)
//print the "test_info" column
String test_info = results.get(0).get('test_info')
println(test_info)
//convert to json format and verify result
def test_infojson = new JsonSlurper().parseText(new String(test_info))
println('Database test_info response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(test_infojson)))
返回的结果为:
{
"testId": "default",
"testLines": [
{
"testId": "TC-1",
"name": "a",
"isDeleted": true
},
{
"testId": "TC-1",
"name": "b",
"isDeleted": false
},
{
"testId": "TC-2",
"name": "c",
"isDeleted": true
},
{
"testId": "TC-2",
"name": "d",
"isDeleted": false
}
],
}
然后我使用断言:
assertThat(test_infojson.testLines[0].name).isEqualTo("b")
这个断言是错误的,因为结果中有2个testId,一个isDeleted = true,另一个false。
正确的断言应该是什么,才能得到"testId":"TC-1“和"isDeleted":false这样断言的值就是”testLines“:"b”
我使用org.assertj.core.api.Assertions.*任何其他assert类都可以。
发布于 2020-06-04 06:06:47
对于JSON断言,我建议使用https://github.com/lukas-krecan/JsonUnit
https://stackoverflow.com/questions/62167368
复制相似问题