Creating testing-friendly Kotlin services with http4k

Re-configurable electric contraption (Photo by John Barkiple via Unplash)
implementation("org.http4k:http4k-core:4.35.3.0")
val service = { request: Request -> Response(OK).body("Hello World") }
val response = service(Request(GET, "/"))

assertThat(response.status, equalsTo(Status.OK)
assertThat(response.bodyString(), equalsTo("Hello World"))

A server is a function, and you can just use it as such.

service.asServer(SunHttp(8000)).start() //starts a server on port 8000
val client = JavaHttpClient()

val response = client(Request(GET, "http://localhost:9000"))

assertThat(response.status, equalsTo(Status.OK)
assertThat(response.bodyString(), equalsTo("Hello World"))
typealias HttpHandler = (Request) -> Response
val service = { request: Request -> Response(OK).body("Hello World") }
val client = service

val response = client(Request(GET, "http://localhost:9000")) //uri is irrelevant

assertThat(response.status, equalsTo(Status.OK)
assertThat(response.bodyString(), equalsTo("Hello World"))
  1. Creating testing data is simple as request and responses are just data classes.
  2. Tests run an order of magnitude faster as they skip the network completely, and are unaffected by port clashes.
  3. There is no separate test infrastructure required for testing. Service setup in tests is identical to production code.
  4. Multiple services can be wired to each other by replacing clients with server implementations. That means tests can cover a whole system as function.
  5. You can reuse test and run the same test in-memory, against servers running on the local machine, or even against a full-blown deployed environment.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store