Write Testable Kotlin Services. Your tests aren’t the problem — it’s… | by Andrew O’Hara | Feb, 2022

Your checks aren’t the issue — it’s your untestable code

Picture by Testalize.me on Unsplash
  • Regression checks guarantee bugs don’t come again
  • Refactor as a lot as you need, as long as the checks nonetheless go
  • A failing check is extra helpful than a passing one

I’ve tried to maintain the code as centered on the principle purpose of this information as potential. I’ve omitted any code that doesn’t straight contribute to that purpose, so the code could seem unfinished or non-robust because of this.

We’re going to make use of two main methods to perform our purpose at this time, mocks and dependency injection.

For the sake of this information, let’s begin from the bottom-up: at our Database Entry Object (DAO) — or Repository. That is our Pet mannequin, and we need to retrieve it from a MySQL database.


The subsequent factor I need to cowl is API purchasers. These may be absolutely examined, with the precise HTTP shopper. I extremely suggest http4k; it’s extremely simple to inject a pretend server right into a shopper, and may run with out utilizing the community.

So I do know we’ve been doing this backwards, however right here is the place we’re lastly going to put in writing some enterprise logic to tie all of it collectively.

Now that now we have enterprise logic in our service, the subsequent step is to supply an interface for purchasers to make use of it. We’ve made our service modular sufficient that the interface may very well be REST, RPC, WebSocket, a stream listener, or perhaps a ViewModel for a local app.

So in spite of everything this time, we’ve fully examined our service, however we haven’t really run it for actual but. No downside. We are able to simply construct a Runner module that gathers our configs builds our RestApi , after which hooks it as much as an actual server.

  • The ThirdPartyImageClient takes an actual shopper because the HttpHandler however wraps it in a Filter that provides the hostname and credentials to the request
  • The RestApi is transformed into an actual server and began

So testing in isolation is all effectively and good, however there have been a number of assumptions we made within the ThirdPartyImageClient and the PetsDao . These assumptions is probably not right, so that is the place just about the one handbook testing is required.

I hope the examples proven on this information assist enhance the testability of your code. As your check protection will increase, you’ll begin to really feel a number of benefits:

  • Much less time wasted on testing means higher improvement velocity
  • Checks create a security web that makes it possible to make massive refactors
  • Higher confidence in deployments and releases

This information was written round a pattern repo, which is obtainable on Github. It could present a extra full image that can assist you make your personal testable code.

You possibly can even mock the AWS SDK! There are two strategies:

  • Begin a pretend AWS server and override the endpoint of your purchasers

Moto is well-known and has two working modes. It might both be built-in straight right into a python software, or it could actually begin a mock server. That is my device of alternative for python functions.

The AWS SDK is de facto only a bloated shopper for his or her REST API, so http4k has a side-project to supply featherweight pre-built purchasers for among the hottest AWS providers. This device appears primarily meant to switch the AWS SDK, however it does provide mock backends for a few of its supported providers. It’s nice for serverless environments since you may substitute Jackson and Apache HttpClient for adapters that don’t have abysmal cold-start efficiency.

I needed a device that was pure java, may very well be injected simply and supported the Dynamo DB Mapper. Not one of the instruments above might give me all of these issues.

More Posts