Writing unit tests in Kotlin, swapping variables?

I am trying to create some functional tests in Kotlin to make requests to a Cart Java service using the Rest Assured library.

Since I want the tests to run procedurally, I was hoping I could save the result of the first API request and pass it on 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 used Kotlin, so looking for guidance on what would be the best way to test all API endpoints works.

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 exchange variables in all tests?

thank

+3


source to share


3 answers


JUnit does not provide any guarantees about the order of testing, and tests should not depend on each other or modify existing state.

The simplest solution is to run two queries in one test.

Alternatively, you can use JUnit test fixtures to set up the objects required for the test suite. If you need cartId

multiple tests, this 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

.

+3


source


Unit tests are intended for independent use. By default, you cannot control the order in which a unit test is executed Read here: How to run test methods in a specific order in JUnit4?

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



That would be correct, we usually call it "Travel Tests"

+2


source


If you have general setup procedures in many tests, I would agree with the suggestions @Before

above, but otherwise you must do each test yourself. Kotlin can make this very pretty - see https://gjesse.github.io/post/unit-testing-with-kotlin---mini-dsls/ for a description of wrapping common operation types for individual tests.

0


source







All Articles