I am trying to create some functional tests in Kotlin to make requests to a Cart Java service using Rest Assured library.
Since I want the tests to behave procedurally I was hoping I could store the result of the first API request and pass it to the next Unit test.
i.e.
createCartTest() --> cartId --> getCartForWebsiteTest(cartId)
class CartTest : RestAssuredSupport {
val port = 8080
val url = "http://localhost:"
val cartId = null
/**
* Create a cart object
*/
@Test fun createCartTest() {
given().
now().
body("websiteId=1").
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/create.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", notNullValue()).
body("summary.version", notNullValue())
}
/**
* Fetch a cart object created by websiteId and cartId
*/
@Test fun getCartForWebsite() {
given().
now().
body("websiteId=1&cartId=" + cartId).
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/getCartForWebsite.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", equalTo(cartId)).
body("summary.version", notNullValue())
}
}
Never really used Kotlin, so looking for advice in what would be the best way to test all the API endpoints are working.
Or would it be better to make another request inside the same function and pass the result to the next step?
What is the best way to share variables across tests?
Thanks