Jus*_*der 5 java rest json rest-assured
我有以下代码,应该测试我的RESTful API:
given().baseUri("http://...").get("/categories/all")
    .then()
    .body(
        "results",  not(empty())
    );
API返回以下响应:
{
    "error": {
        "type": "NotFoundException"
    }
}
我希望测试失败以获得此类响应.但是测试通过了.
如何修改测试以使其失败?只有当API返回一个在"results"键中包含非空数组的对象时,它才会通过.当"results"键不存在,包含空数组或包含不是数组的内容时,它应该失败.
我提出了以下解决方案:
given().baseUri("http://...").get("/categories/all")
    .then()
    .body(
        "results", hasSize(greaterThan(0))
    );
如果"results"是空数组或不是数组,则失败.如果"results"是非空数组,它会通过.它以可读的方式报告错误,例如:
Expected: a collection with size a value greater than <0>
Actual: null
我遇到了类似的问题,但在我的情况下,端点直接返回一个数组。我对此的解决方案:
@Test
public void testNotEmpty() {
    uAssured.given()
            .when()
                .get("resources/totest")
            .then()
                .statusCode(200)
                .body("$.size()", greaterThan(0));
}
对于上面的示例,以下内容也应该有效:
@Test
public void testNotEmpty() {
    uAssured.given()
            .when()
                .get("resources/totest")
            .then()
                .statusCode(200)
                .body("results.size()", greaterThan(0));
}
| 归档时间: | 
 | 
| 查看次数: | 8915 次 | 
| 最近记录: |