assert - 如何获取带条件返回的结果并断言正确的值
问题描述
我编写了代码来断言从数据库存储的值。
//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。
什么应该是正确的断言,以获取 testLines 其中 "testId": "TC-1" 和 "isDeleted": false 所以断言值为 "name": "b"
我正在使用 org.assertj.core.api.Assertions.* 任何其他断言类都可以。
解决方案
对于 JSON 断言,我建议使用https://github.com/lukas-krecan/JsonUnit
推荐阅读
- javascript - 如何通过 2 个不同的链接以不同的方式显示网页?
- c# - 数组输出我没有输入的随机数
- python - 有什么方法可以在不更改 python 中的 dtype 的情况下转换数据框中的日期时间格式
- asp.net - 由于来自 IIS ASP.NET 网站的非法 URL,事件查看器向事件 1309 (HttpException) 发送垃圾邮件
- c# - 如何在循环 C# 中插入列表时提高性能
- arrays - 值列表上的计算字段
- javascript - 了解 http 请求如何在 Mocha 上工作
- sql-server - 获取权限错误和“功能不存在”错误
- perl - 如何在 Perl 中使用 mkdir 创建文件夹?
- c++ - 为什么我的代码在输出中添加了 93 天?