- Programming Language: JAVA
- IDE: Eclipse
- Create a Maven project to build the program;
- Add all dependences in pom.xml regarding some frameworks that I used such as Cucumber to manage BDD, Rest-assured to call the web-service REST and added a json-schema-validator to validate JSON response format;
BDD requires a feature file to invoke the step definitions:
- Create the scenarios in feature file as per the requirements, so each step in feature file has to match a step definition in class file;
- Following the BDD practices for coding;
- Using the special annotation like "@Before" which is the first method to run for each scenario. Moreover, this is the right place to set up the URI (endpoint) which will be called by HTTP request;
In order to test REST APIs, I found REST Assured library so useful:
- First, I performed the manual tests using POSTMAN plugin in chrome browser.
- This project is aimed at calling Correios' API to validate the CEP. This is written in a feature file using Cucumber.
- Each line of the scenario is tied to backend code that actually executes the line (step).
Testing a simple response containing some JSON data.
- Request URL: http://correiosapi.apphb.com/cep/13040089
- Request Method: GET
- Response Content-Type: application/json
- Response Body: { "cep": "13040089", "tipoDeLogradouro": "Rua", "logradouro": "Manoel Sylvestre de Freitas Filho", "bairro": "Jardim Nova Europa", "cidade": "Campinas", "estado": "SP" }
- Status Code: 200 OK
- Request URL: http://correiosapi.apphb.com/cep/12345678
- Request Method: GET
- Response Body: { "message": "Endereço não encontrado!" }
- Status Code: 404 Not Found
- Th main goal is to ensure that the JSON format is correct as well as all data inside him. Therefore, it was created a json file (schema) and stored it in "resource" file in the package project. In the source-code is validated by the statement 'body(matchesJsonSchemaInClasspath("schema-json.json"))';
- when().get("/" + cep)
- then().log().all();
- then().statusCode(Integer.parseInt(statusCode));
- then().assertThat().contentType(ContentType.JSON).and().body(matchesJsonSchemaInClasspath("schema-json.json"));