Logo

dev-resources.site

for different kinds of informations.

Reducing Boilerplate in Vert.x Tests written in Kotlin

Published at
5/4/2021
Categories
vertx
kotlin
testing
java
Author
wowselim
Categories
4 categories in total
vertx
open
kotlin
open
testing
open
java
open
Author
8 person written this
wowselim
open
Reducing Boilerplate in Vert.x Tests written in Kotlin

In the previous part, we saw how Kotlin can reduce noise in asynchronous tests. In this part we will introduce some syntactic sugar that will help us avoid repetition in our Verticle tests.

When testing a Verticle, we need to tell junit to deploy the Verticle before the test, and undeploy it afterwards. This is fairly straightforward but it can be tedious to add to every single test file:

  private lateinit var deploymentId: String

  @BeforeEach
  fun deployVerticle(): Unit = runBlocking(vertx.dispatcher()) {
    deploymentId = vertx.deployVerticle(verticleClass.java, DeploymentOptions()).await()
  }

  @AfterEach
  fun undeployVerticle(): Unit = runBlocking(vertx.dispatcher()) {
    vertx.undeploy(deploymentId).await()
  }
Enter fullscreen mode Exit fullscreen mode

There's a very simple way we can avoid this boilerplate. If we move this into an abstract class that does this once, we can simply extend it and focus on our tests instead.

We should make sure that this is flexible enough so in this case we want some configuration options. We should be able to:

  • Specify which Verticle we would like to test
  • Pass configuration to this Verticle such as a database URI or a temporary directory
  • Customize the deployment if necessary
  • Run test code on Vert.x threads

So let's define this helper class:

abstract class AsyncTest(
  private val vertx: Vertx,
  private val verticleClass: KClass<out Verticle>,
  private val deploymentOptions: DeploymentOptions = DeploymentOptions()
) {
  private lateinit var deploymentId: String

  open fun deployVerticle(): String = runBlocking(vertx.dispatcher()) {
    vertx.deployVerticle(verticleClass.java, deploymentOptions).await()
  }

  @BeforeEach
  fun assignDeploymentId() {
    deploymentId = deployVerticle()
  }

  @AfterEach
  fun undeployVerticle(): Unit = runBlocking(vertx.dispatcher()) {
    vertx.undeploy(deploymentId).await()
  }

  protected fun runTest(block: suspend () -> Unit): Unit = runBlocking(vertx.dispatcher()) {
    block()
  }
}
Enter fullscreen mode Exit fullscreen mode

If we have things like Http clients, we can also put them in here and make them accessible to all tests. Now, when we want to test a Verticle, we simply extend this class add tests:

@ExtendWith(VertxExtension::class)
class TestMainVerticle(vertx: Vertx) : AsyncTest(vertx, MainVerticle::class) {

  @Test
  fun `service is healthy when deployed`() = runTest {
    val request = httpClient.request(HttpMethod.GET, 8080, "localhost", "/health").await()
    val response = request.send().await()
    val responseJson = response.body().await().toJsonObject()

    assertEquals("up", responseJson.getString("status"))
  }
}
Enter fullscreen mode Exit fullscreen mode

Our helper class will make sure that:

  • The verticle is deployed & undeployed for each test
  • Tests wrapped in runTest run on a Vert.x thread automatically

Summary

In this part, we looked at how to further reduce boilerplate in our asynchronous tests. In the next part, we will see how we can combine everything we've learned so far in order to write integration tests using Testcontainers.

Source

A full project can be found in this GitHub repo.

vertx Article's
28 articles in total
Favicon
Error handlers and failure handlers in Vert.x
Favicon
Why we discarded Reactive systems architecture from our code?
Favicon
Build web application in Vert.x [Part 1/ ♾️]
Favicon
Yet another ode to Vert.x, or how to write a performance-wise expiring map in less than 100 lines of code.
Favicon
Surprising Qualities of Event Driven System
Favicon
Idiomatic Kotlin Abstractions for the Vert.x EventBus
Favicon
Vert.x Circuit Breaker
Favicon
Writing Async Tests for Vert.x using Kotlin
Favicon
Reducing Boilerplate in Vert.x Tests written in Kotlin
Favicon
Writing Vert.x Integration Tests with Kotlin & Testcontainers
Favicon
Quarkus: Entendendo a relação entre o Mutiny e o Vert.x
Favicon
HTTPS Client Certificate Authentication With Java
Favicon
Throttle HTTP requests on paged resources with Vert.x
Favicon
Supercharge Your Kotlin Vert.x Application with EventBus Extensions
Favicon
Handle backpressure between Kafka and a database with Vert.x
Favicon
Handling unknown JSON structures
Favicon
Introduction to Vert.x
Favicon
Future Composition in Vert.x
Favicon
How to extend Vert.x EventBus API to save on serialization.
Favicon
How to write beautiful unit tests in Vert.x
Favicon
Scaling Vert.x application for session dependent data processing.
Favicon
KVision v3.7.0 is released (with Vert.x support)
Favicon
Reactive Java using the Vert.x toolkit
Favicon
Vert.x Kotlin Coroutines
Favicon
How we built a RESTful API with Vert.x, Kotlin Coroutines and Keycloak
Favicon
vertx-jooq 2.4 released
Favicon
Sirix - Released 0.9.1 (time travel queries and versioning made easy)
Favicon
Reactive Programming with Kotlin - Quick Intro to Vert.x

Featured ones: