javarest-assured

Rest Assured: How to use "assertThat().body("email", is(email)); to validate some response?


I'm trying to validate the JSON response of a POST API that I'm developing the Automation Tests.

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

But my code doesn't recognize the "IS" on the last line, I tried to change it to "ContainsString", "equalsTo", but it returns the same error. I don't know if any dependency or some import statement missing. Can you help me? Thanks!


Solution

  • You need to do a static import of 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));
        }
    

    (OR) use import org.hamcrest.CoreMatchers; and CoreMatchers.is(email)