You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

接口响应断言异常:Result.Success值匹配失败求助

接口测试断言失败问题解决

你的问题核心是类型不匹配:响应JSON里的Result.Success是布尔类型的true,但你断言代码里用了字符串类型的"true"进行匹配,尽管错误提示里的预期和实际看起来都是true,但匹配器会严格校验数据类型,因此导致断言失败。

解决方法

直接将断言中的字符串"true"改为布尔值true即可,修改后的断言代码如下:

given().body(request.toJSONString()).header("Content-Type", sitedata.getProperty("Content-Type"))
        .header("ApplicationCode", sitedata.getProperty("ApplicationCode"))
        .header("Authorization", sitedata.getProperty("Authorization")).when()
        .post(sitedata.getProperty("BaseUrl") + sitedata.getProperty("login"))
        .then().log().all().statusCode(200)
        .body(Matchers.containsString("Success"))
        .body("Result.Success", Matchers.equalTo(true)); // 去掉引号,使用布尔值true

你也可以用更简洁的Matchers.is(true)替代Matchers.equalTo(true),效果完全一致:

.body("Result.Success", Matchers.is(true));

补充说明

JSON中的值分为不同类型,true是布尔类型,而"true"是字符串类型,接口测试框架的匹配器默认会做严格的类型校验,所以即使字面量相同,类型不一致也会断言失败。

内容的提问来源于stack exchange,提问作者Krina Modi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 08:35:24