Hot questions for Using Cucumber in rest assured

Top Java Programmings / Cucumber / rest assured

Question:

I'm trying to create a Test Automation for a POST API using Rest-Assured and Java.

This API has the follow body as Application/JSON:

{
    "billing_address": {
        "region": "Sao Paulo",
        "region_id": 123,
        "country_id": "BR",
        "street": ["Av Paulista"],
        "postcode": "10577",
        "city": "SAO PAULO",
        "telephone": "555-555-1111",
        "company": "Test test",
        "firstname": "Fernando",
        "lastname": "Alves",
        "document": "0123444064"
     }
}

In my code, I developed the follow method to send this values and the request:

public void criarPF (String srtAmbiente, String srtAPI, String srtToken, String srtRegion, String srtRegionID, String srtCountryID, String srtStreet,
                    String srtPostcode, String srtCity, String srtTelephone, String srtCompany, String srtFirstname, String srtLastname, String srtDocument) {

                    String uriBase = srtAmbiente;
                    String Api = srtAPI;

                    Map<String, String> addressContent = new HashMap<String,String>();
                    addressContent.put("region", srtRegion);
                    addressContent.put("region_id", srtRegionID);
                    addressContent.put("country_id", srtCountryID);
                    addressContent.put("street", srtStreet);
                    addressContent.put("postcode", srtPostcode);
                    addressContent.put("city", srtCity);
                    addressContent.put("telephone", srtTelephone);
                    addressContent.put("company", srtCompany);
                    addressContent.put("firstname", srtFirstname);
                    addressContent.put("lastname", srtLastname);
                    addressContent.put("document", srtDocument);


                    Map<String, Object> postContent = new HashMap<String,Object>();
                    postContent.put("billing_address", addressContent);


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

I received an error messenger that "street" isn't a String, it's a String[]. So, how can I send a String and String[] values in the same API's body. Thanks!


Answer:

You are setting street to a string value. Just change it like this:

Map<String, Object> addressContent = new HashMap<>();
addressContent.put("region", srtRegion);
addressContent.put("region_id", srtRegionID);
addressContent.put("country_id", srtCountryID);

// Here wrap strStreet in a List
addressContent.put("street", Arrays.asList(srtStreet));

addressContent.put("postcode", srtPostcode);
addressContent.put("city", srtCity);
addressContent.put("telephone", srtTelephone);
addressContent.put("company", srtCompany);
addressContent.put("firstname", srtFirstname);
addressContent.put("lastname", srtLastname);
addressContent.put("document", srtDocument);

Question:

I'm trying to create an Test Automation for a POST API using Rest-Assured and Java. This POST API have a body as Application/JSON, like this:

{
    "customer":{
        "email": "teste@mailinator.com"
    },
    "password":"Teste@12"
}

To make this request I'm using the follow code, but it's returning Status code "400", but I'm sending the same information on Postman and it's returning "200":

@And("envio as informacoes da chamada: (.*), (.*), (.*), (.*) e (.*)")
        public void enviarDados (String srtEmail, String srtSenha, String srtAmbiente, String srtAPI, String srtToken) {
HashMap<String,String> postContent = new HashMap<String,String>();
            postContent.put("email", srtEmail);
            postContent.put("password", srtSenha);
            //System.out.println("{\"customer\":" +postContent+ "}");
            given().contentType(ContentType.JSON).header("Authorization", "Bearer"+srtToken).header("Content-Type", "application/json").
            //with().body(postContent).
            with().body("{\"customer\":" +postContent+ "}").
            when().post(srtAmbiente+srtAPI).
            then().statusCode(200); 
}

The "400" response is:

{
"status": 400,
"message": "Malformed request",
"additional_error": ""

}

Is the structure right? There is something you think is missing? Thanks!


Answer:

You are sending an incorrect body with POST.

//This line will not serialize HashMap to JSON, but call toString()
.body("{\"customer\":" +postContent+ "}")

As a result your payload will look this way:

{"customer":{password=password, customer=example@example.com}}

which is not a valid JSON. Try this:

Map<String, String> emailContent = new HashMap<>();
emailContent.put("email", "example@example.com");
Map<String, Object> postContent = new HashMap<>();
postContent.put("customer", emailContent);
postContent.put("password", "password");
given().contentType(ContentType.JSON)
    .header("Authorization", "Bearer "+srtToken)
    .with().body(postContent)
    .when().post(srtAmbiente+srtAPI)
    .then().statusCode(200); 

Question:

I am trying to use Cucumber and REST Assured to create autoamted tests in my Gradle project.

Below is the XML Response Body:

<ValidationResponse>
    <errors>
        <error>
            <field>id</field>
        </error>

        <error>
            <field>amount</field>
        </error>

    </errors>
</ValidationResponse>

I am trying to use the below REST Assured code to check that:

  • "id" appears in the first error field
  • "amount" appears in the second error field

    RestAssured.given()
    .auth()
    .preemptive()
    .basic(theUsername, thePassword)
    .contentType(theContentType)
    .header("Accept",theContentType)
    .body(theXMLBody)
    .when()
    .post(theURL)
    .then()
    .body("ValidationResponse.errors.error[0].field", equalTo("id"))
    .and()
    .body("ValidationResponse.errors.error[1].field", equalTo("amount"));
    

The second field is failing because the code is just checking the first "field" it runs into.

Does anybody know what changes I need to make to my code so that it checks the second "field" also?


Answer:

Use below snippet to validate the response fields.

String responseXMLAsString = "<ValidationResponse>     <errors>         <error>             <field>id</field>         </error>          <error>             <field>amount</field>         </error>      </errors> </ValidationResponse>";
    XmlPath xmlPath = new XmlPath(responseXMLAsString);
    assertThat(xmlPath.get("ValidationResponse.errors.error.field"), contains("id","amount"));

OR

RestAssured.given()
.auth()
.preemptive()
.basic(theUsername, thePassword)
.contentType(theContentType)
.header("Accept",theContentType)
.body(theXMLBody)
.when()
.post(theURL)
.then()
.body("ValidationResponse.errors.error.field", contains("id","amount"));

Where contains() is hamcrest matcher

import static org.hamcrest.collection.IsIterableContainingInOrder.contains;