Testing a gateway client using Javalin
In Kotlin/Java, how would you test that your app is properly consuming an external REST API? We’ll use Javalin to do it.

📝 I adapted this article to the official Javalin docs page.
My proposal is to use Javalin (a lightweight webserver) as the test double, thereby replacing the external API (the DOC: dependent-on component). We’ll launch Javalin acting as the real API but running in localhost so that the gateway (the SUT) can’t tell the difference. We’ll confirm the validity by asserting the calls made to the test double.

⚠️ Don’t confuse testing API calls with testing your own API handlers. We want to tackle the former: testing a REST client; not a REST service.
Before starting
We’ll have two examples of testing ProfileGateway: a query and a command, according to the command/query separation:
- query: check that it properly consumes a GET response from an external party; we’ll assert the output of a method;
- command: check that a POST call was made as expected; we’ll assert a consequence, namely the posted body.
We just need this boilerplate that guarantees that Javalin stops per every test:





