WireMock and Kotlin

Disclaimer: This solution page includes resources maintained by their authors outside the WireMock organization on GitHub. The info is provided as is without guarantees unless explicitly specified. Referenced projects and guidelines might be incompatible with the recent WireMock 3.x releases or the recent versions of the target technology or framework.

Please use with caution, and submit GitHub issues or pull requests to this page and referenced repositories if something is missing, broken or needs update.

Kotlin DSL Bindings #

There is a Kotlin WireMock library that provides handy Kotlin DSL bindings for WireMock. Note that this library is maintained outside the WireMock organization on GitHub.

Example:

wiremock.get {
    url equalTo "/users/1"
} returns {
    statusCode = 200
    header = "Content-Type" to "application/json"
    body = """
    {
      "id": 1,
      "name": "Bob"
    }
    """
}

Kotest Extension #

Kotest is a popular Kotlin test framework that provides assertions library, property testing and more. There is a Kotest extension for WireMock that integrates WireMock into the framework. Note that this library is maintained by the Kotest community.

Example:

class SomeTest : FunSpec({
  val customerServiceServer = WireMockServer(9000)
  listener(WireMockListener(customerServiceServer, ListenerMode.PER_SPEC))

  test("let me get customer information") {
    customerServiceServer.stubFor(
      WireMock.get(WireMock.urlEqualTo("/customers/123"))
        .willReturn(WireMock.ok())
    )

    val connection = URL("http://localhost:9000/customers/123").openConnection() as HttpURLConnection
    connection.responseCode shouldBe 200
  }

    //  ------------OTHER TEST BELOW ----------------
})

References: