放心:如何使用 "assertThat().body("email", is(email)); 来验证某些响应?

问题描述 投票:0回答:1

我正在尝试验证我正在开发自动化测试的 POST API 的 JSON 响应。

public void criarConta(String srtAmbiente, String srtAPI, String srtToken, String srtSenha) {
            System.out.println(srtAmbiente+srtAPI);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int length = 15;
            String email = generateRandomEmail(length);
            System.out.println(email);
            Map<String, String> emailContent = new HashMap<String,String>();
            emailContent.put("email", email);
            Map<String, Object> postContent = new HashMap<String,Object>();
            postContent.put("customer", emailContent);
            postContent.put("password", srtSenha);
            RestAssured.given().contentType(ContentType.JSON)
            .header("Authorization", "Bearer "+srtToken)
            .with().body(postContent)
            .when().post(srtAmbiente+srtAPI).prettyPeek()
            .then().statusCode(200).contentType(ContentType.JSON)
            .assertThat().body("email", *is*(email));
    }

但是我的代码无法识别最后一行的“IS”,我尝试将其更改为“ContainsString”,“equalsTo”,但它返回相同的错误。我不知道是否缺少任何依赖项或某些导入语句。 你能帮助我吗?谢谢!

java rest-assured
1个回答
2
投票

您需要静态导入

is()

import static org.hamcrest.CoreMatchers.is;

RestAssured.given().contentType(ContentType.JSON)
                .header("Authorization", "Bearer "+srtToken)
                .with().body(postContent)
                .when().post(srtAmbiente+srtAPI).prettyPeek()
                .then().statusCode(200).contentType(ContentType.JSON)
                .assertThat().body("email", is(email));
    }

(或)使用

import org.hamcrest.CoreMatchers;
CoreMatchers.is(email)

© www.soinside.com 2019 - 2024. All rights reserved.