Hamcrest - Is there any way to ignore fields when we use a file as the basis for response body validation?

0
Hello, I'm starting in the automation world and I was left with a question, in case I'm validating the POST insertion in a REST API that returns information to me in the response body, but some of these fields in the response body change (ID and Type) .

Follow my code

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import static org.hamcrest.Matchers.equalTo;

public class APICriaContatoTests {

/*VARIAVEIS PARA A REQUEST*/

@Test
public void insereInformandoTodosOsDados()
{
    JsonPath expectedJson = new JsonPath(new File("arquivos\jsonInsereInformandoTodosOsDados.json"));

    //Criando dados para a requisição

    jsonAsMap.put("name", "Teste de Nome 4");
    jsonAsMap.put("last-name", "Nome Completo 4");
    jsonAsMap.put("email", "[email protected]");
    jsonAsMap.put("age", "25");
    jsonAsMap.put("phone", "5519864666664");
    jsonAsMap.put("address", "Rua 4");
    jsonAsMap.put("state", "Estado 4");
    jsonAsMap.put("city", "Cidade 4");

    //executando a requisicao

    RestAssured.given().accept(ACCEPT).contentType(TYPE)
    .body(jsonAsMap)
    .when()
    .post(ENDPOINT_POST_CRIA_CONTATOS)
    .then()
    .statusCode(201)
    .body("", equalTo(expectedJson.getMap("")));
}
}

Data from my file

{
  "data": {
    "id": "12",
    "type": "contacts",
    "attributes": {
  "name": "Teste de Nome 4",
  "last-name": "Nome Completo 4",
  "email": "[email protected]",
  "age": 25,
  "phone": "5519864666664",
  "address": "Rua 4",
  "state": "Estado 4",
  "city": "Cidade 4"
    }
  }
}

As the body validation I am using a sample file, I would like to know:

  • Is there a way to use hamcrest to skip some fields when doing the assert?
  • Is there a better way to do this validation (validate the response body by ignoring some fields)?
  • Thank you!

        
    asked by anonymous 08.11.2018 / 01:34

    0 answers