3

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

tomaytotomato
  • 3,788
  • 16
  • 64
  • 119

3 Answers3

3

JUnit does not provide any guarantees about the tests ordering, and the tests should not depend on each other nor mutate the shared state.

The easiest solution is, indeed, to perform the two requests in the same test.

Also, you can use JUnit test fixtures to set up the objects you need for a set of tests. If you need the cartId in several tests, that is the preferred option.

class CartTest : RestAssuredSupport {
    // ...
    var cartId: Int by Delegates.notNull()

    @Before fun setUp() {
        // Set up the cartId. This will run before each test. Use @BeforeClass to run once
    }

    @Test fun getCartForWebsite() { /* ... */ }
}

See: Delegates.notNull(), another question about @Before and @BeforeClass.

Community
  • 1
  • 1
hotkey
  • 140,743
  • 39
  • 371
  • 326
2

Unit tests are meant to be self contained. By default you cannot control the order of execution of unit test Have read here : How to run test methods in specific order in JUnit4?

"Or would it be better to make another request inside the same function and pass the result to the next step?"

That would be correct, we usually call it Journey Tests

Community
  • 1
  • 1
ian1095
  • 551
  • 4
  • 8
0

If you have common setup procedures across many tests I would tend to agree with @Before suggestions above, but otherwise you should make each test self contained. Kotlin can make this very nice - see https://gjesse.github.io/post/unit-testing-with-kotlin---mini-dsls/ for a description of wrapping common operation types for individual tests.

jhodges
  • 3,065
  • 2
  • 17
  • 9