如何在test包测试用例类中调用helping包错误信息类的静态字符串参数
测试断言实现方案
你已经完成了prefundErrorMessages类的导入操作,该类中的错误提示是public static修饰的静态常量,直接通过类名调用常量,和接口返回的description字段做等值断言即可。
具体修改步骤
在你标注注释的位置添加如下断言代码即可:
// 校验返回错误描述和预设值一致,第三个参数为断言失败时的提示信息,方便排查问题 Assert.assertEquals(description, prefundErrorMessages.error_description_when_special_character_inserted_in_product_code, "接口返回的错误描述不符合预期");
可选优化点
- 先对
description做非空校验,避免空指针异常导致断言失败原因不清晰:
Assert.assertNotNull(description, "接口返回的description字段为空");
- 遵循Java编码规范,将
prefundErrorMessages类名调整为大驼峰格式PrefundErrorMessages,提升代码可读性。 - 注意你当前贴出的代码中
@BeforeClass修饰的setup方法缺少闭合大括号,需要补充否则代码无法正常编译运行。
修改后完整的测试方法代码
@Test(description="validate that BVA and EQP for Product code field in request") public void validateproductCode() { logger.info("Checking BVA and EQP for Product code "); product.setProduct_code("Test@123" + generatedString); response=given().header("Authorization",productauth).and().body(product).when () .patch("/product"); int statuscode = response.getStatusCode(); Assert.assertEquals(statuscode, 400); String responsebody = response.getBody().asString(); Assert.assertTrue(responsebody != null); String description=response.jsonPath().getString("description"); // 新增的断言代码 Assert.assertNotNull(description, "接口返回的description字段为空"); Assert.assertEquals(description, prefundErrorMessages.error_description_when_special_character_inserted_in_product_code, "接口返回的错误描述不符合预期"); }
内容的提问来源于stack exchange,提问作者Amit Bhalla
相关产品推荐
相关产品推荐

