Testing REST services
Verificação e Validação de Software Departamento de Informática Faculdade de Ciências da Universidade de Lisboa
Vasco T. Vasconcelos
Testing REST services Verificao e Validao de Software Departamento - - PowerPoint PPT Presentation
Testing REST services Verificao e Validao de Software Departamento de Informtica Faculdade de Cincias da Universidade de Lisboa Vasco T. Vasconcelos Some tools Web browser: allows only for GET operations For the remaining
Verificação e Validação de Software Departamento de Informática Faculdade de Ciências da Universidade de Lisboa
Vasco T. Vasconcelos
allows to save common requests)
for multiple platforms
for persistence)
specs
coined by Eric Evans and Martin Fowler) is an implementation of an object oriented API that aims to provide more readable code (https:// en.wikipedia.org/wiki/Fluent_interface)
import static com.jayway.restassured.RestAssured.get; @Test public void testWelcomeStatusCode() { get(“http://localhost:8080/v1/lotto"). then(). statusCode(200); }
Verb Path In the Response header Success (better: use a constant)
Note the indentation
import static com.jayway.restassured.RestAssured.baseURI; @BeforeClass public static void testSetup() { baseURI = "http://localhost:8080/lotto"; // Setup the database } @Test public void testWelcomeStatusCode() { get(). then(). statusCode(200); }
Implicit path
{ "lotto":{ "lottoId":5, "winning-numbers":[2,45,34,23,7,5,3], "winners":[{ "winnerId":23, "numbers":[2,45,34,23,3,5] },{ "winnerId":54, "numbers":[52,3,12,11,18,22] }] } }
import static org.hamcrest.Matchers.equalTo; @Test public void testLottoIdIs5() { get().then().body("lotto.lottoId", equalTo(5));; }
An hamcrest Matcher A path in JSON
import static org.hamcrest.Matchers.hasItems; @Test public void testLottoHasItems() { get(). then(). body(“lotto.winners.winnerId", hasItems(23, 54)); }
Another hamcrest Matcher
expressions of intent”
hamcrest.org/JavaHamcrest/javadoc/1.3/org/ hamcrest/Matchers.html
@Test public void testPOSTPet() { Map<String, Object> pet = new HashMap<>(); pet.put("id", 32); pet.put("name", "Felix"); pet.put("status", “alive"); given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201); }
Created JSON with 3 properties Specify the content type
import javax.json.Json; @Test public void testPOSTPetBuilder() { String pet = Json.createObjectBuilder(). add("id", 32). add("name", "Felix"). add("status", "alive"). build().toString(); given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201); }
use
HTTP 201 Created Location: http://localhost:8080/v1/258 Content-Type: text/html Date: Mon, 16 May 2016 11:09:28 GMT Content-Length: 74 X-Powered-By: Undertow/1 Connection: keep-alive Server: WildFly/10
@Test public void testGet() { String pet = Json.createObjectBuilder()…; String location = given(). contentType("application/json"). body(pet). when(). post(). then(). statusCode(201). extract().header("Location"); get(location).then().statusCode(200); }
Created Extract the string under property Location Now check that the pet was properly POSTed Extract from the header