testassured断言大全

TestAssured是一个流行的Java库,用于进行接口测试和断言验证。它提供了丰富的断言方法来验证API的响应结果。以下是TestAssured中一些常用的断言方法和用法:

  1. 验证状态码:
given().
    when().
    get("/api/user").
    then().
    statusCode(200); // 验证状态码是否为200
  1. 验证响应体内容:
given().
    when().
    get("/api/user").
    then().
    body("username", equalTo("john")); // 验证响应体中的username字段值是否为"john"
  1. 验证数组响应体内容:
given().
    when().
    get("/api/users").
    then().
    body("name", hasItems("John", "Jane")); // 验证数组响应体中是否包含"John"和"Jane"
  1. 验证响应头:
given().
    when().
    get("/api/user").
    then().
    header("Content-Type", "application/json"); // 验证响应头中Content-Type是否为"application/json"
  1. 验证响应时间:
given().
    when().
    get("/api/user").
    then().
    time(lessThan(5000L)); // 验证响应时间是否小于5秒
  1. 验证响应体json路径:
given().
    when().
    get("/api/user").
    then().
    body("address.city", equalTo("New York")); // 验证响应体中的address.city字段值是否为"New York"
  1. 验证响应体json数组长度:
given().
    when().
    get("/api/users").
    then().
    body("size()", equalTo(10)); // 验证响应体中的json数组长度是否为10

这只是TestAssured中一小部分常用的断言方法,你可以根据需要,选择合适的方法来验证接口的响应结果。

希望这些内容能对你有所帮助!如果有其他问题,请随时提问。