简体   繁体   中英

How can I validate a Rest-Assured request in another method?

I developed the Rest-Assured request on the dataMap class, but I need to validate this request in another method to call the cucumber steps of my feature.

My cucumber feature:

Esquema do Cenario: Criar uma conta valida
        Dado que realizo a chamada no <ambiente> da <api> informando <token_admin> e um email e <senha> novos
        Entao devera retornar <status code>

I tried to do it this way:

public class dataMap extends tests {

private Response response;
private ValidatableResponse json;
private RequestSpecification request;

    public void criarConta(String srtAmbiente, String srtAPI, String srtToken, String srtSenha) {

        String uriBase = srtAmbiente;
        String Api = srtAPI;

        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);

        request = RestAssured.given().contentType(ContentType.JSON)
                .header("Authorization", "Bearer "+srtToken).with().body(postContent);

        response = request.when().post(uriBase+Api).prettyPeek();

    }

    public void responseCriarContaStatus (String srtStatusCode) {
        json = response.then().statusLine("HTTP/1.1 200 OK");

But the responseCriarContaStatus method is not working. When I insert the json = response.then().statusLine("HTTP/1.1 200 OK"); into the criarConta method the code works.

How can I validate this request in the responseCriarContaStatus method?

You can set Response object static and after defining the response you can use it in another method. I am sharing you my own method then you can edit it. statusCode will be 200 if request get success.

private static Response response;

public void responseCriarContaStatus (String srtStatusCode) {
        JSONObject json = new JSONObject(response.getBody().asString); 
        Asser.asserEquals(srtStatusCode , json.getString("statusCode"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM