英文:
Groovy API Response JSON object assertion
问题
API响应
[数据:[重复发票引用:null,请求ID:null],错误:[[代码:API.INV.005,描述:发票引用为空]],状态:3,警告:null]
我将对错误对象进行断言,以下是我的代码
def responseData = jsonSlurper.parseText(responseBody)
assert responseData.Error.Description == "发票引用为空"
但是我收到了断言失败
assert responseData.Error.Description == "发票引用为空"
| | | |
| | | false
| | [发票引用为空]
| [[代码:API.INV.005,描述:发票引用为空]]
[数据:[重复发票引用:null,请求ID:null],错误:[[代码:API.INV.005,描述:发票引用为空]],状态:3,警告:null]
请阐明断言部分的问题?
英文:
API Response
[Data:[DuplicateInvoiceReference:null, RequestID:null], Error:[[Code:API.INV.005, Description:Invoice Reference is null or empty]],
Status:3, Warning:null]
I'm going to asset the Error Object, below is my code
def responseData = jsonSlurper.parseText(responseBody)
assert responseData.Error.Description == "Invoice Reference is null or empty"
but I received assertion failed
assert responseData.Error.Description == "Invoice Reference is null or empty"
| | | |
| | | false
| | [Invoice Reference is null or empty]
| [[Code:API.INV.005, Description:Invoice Reference is null or empty]]
[Data:[DuplicateInvoiceReference:null, RequestID:null], Error:[[Code:API.INV.005, Description:Invoice Reference is null or empty]], Status:3, Warning:null]
Please clarify the issues in the assertion part?
答案1
得分: 2
你似乎把 Description
当作 Error
是一个映射(map)来阅读。实际上,它是一个列表。
你应该这样写:
assert responseData.Error[0].Description == "Invoice Reference is null or empty"
你需要从 responseData.Error
的第一个元素中读取 Description
。
英文:
You're reading Description
as though Error
were a map. It is a list.
You should be having
assert responseData.Error[0].Description == "Invoice Reference is null or empty"
You need to read Description
off the first element of responseData.Error
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论