Send web requests and assert results with vertx-junit5-web-client

In the last Vert.x 3.8 re­lease, we added a new mod­ule called vertx-junit5-web-client, that brings Vert.x Web Client in­jec­tion into tests and pro­vides an API called TestRequest to sim­plify the cre­ation and as­ser­tions on WebClient re­quests:

import static io.vertx.junit5.web.TestRequest.*;

@ExtendWith({
  VertxExtension.class, // VertxExtension MUST be configured before VertxWebClientExtension
  VertxWebClientExtension.class
})
public class TestRequestExample {

  @Test
  public void test1(WebClient client, VertxTestContext testContext) {
    testRequest(client, HttpMethod.GET, "/hello") // Build the request
      .with(
        queryParam("name", "francesco"), // Add query param
        requestHeader("x-my", "foo") // Add request header
      )
      .expect(
        // Assert that response is a JSON with a specific body
        jsonBodyResponse(new JsonObject().put("value", "Hello Francesco!")),
        // Assert that response contains a particular header
        responseHeader("x-my", "bar")
      )
      .send(testContext); // Complete (or fail) the VertxTestContext
  }

}

testRequest() will use Vert.x Web Client to send the re­quest. When the re­sponse is re­ceived, It suc­ceds the test or it cor­rectly prop­a­gates as­ser­tion fail­ures, if any.

You can also send mul­ti­ple re­quests using Checkpoint:

import static io.vertx.junit5.web.TestRequest.*;

@ExtendWith({
  VertxExtension.class, // VertxExtension MUST be configured before VertxWebClientExtension
  VertxWebClientExtension.class
})
public class MultiTestRequestExample {

  @Test
  public void test2(WebClient client, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(2); // Create the Checkpoint to flag when request succeds

    testRequest(
        client    // Create the test request using WebClient APIs
          .get("/hello")
          .addQueryParam("name", "francesco")
          .putHeader("x-my", "foo")
      )
      .expect(
        jsonBodyResponse(new JsonObject().put("value", "Hello Francesco!")),
        responseHeader("x-my", "bar")
      )
      .send(testContext, checkpoint); // Pass the checkpoint to flag

    testRequest(
        client
          .get("/hello")
          .addQueryParam("name", "julien")
          .putHeader("x-my", "foo")
      )
      .expect(
        jsonBodyResponse(new JsonObject().put("value", "Hello Julien!")),
        responseHeader("x-my", "bar")
      )
      .send(testContext, checkpoint);
  }

}

Look at Vert.x JUnit 5 Web Client doc­u­men­ta­tion for more de­tails

Next post

Eclipse Vert.x 3.8.4

This version is a bug fix release of Vert.x 3.8.3, which addresses quite a few bugs reported by the community.

Read more
Previous post

Eclipse Vert.x 3.8.3

This new version is a minor bug fix release that addresses issues reported in Eclipse Vert.x 3.8.2. We would like to thank you all for reporting these bugs.

Read more
Related posts

Unit and Integration Tests

Let’s refresh our mind about what we developed so far in the introduction to vert.x series. We forgot an important task. We didn’t test the API.

Read more

Building services and APIs with AMQP 1.0

Microservices and APIs are everywhere. Everyone talks about them, presentation slides are full of them ... some people are actually even building them.

Read more

Eclipse Vert.x 4 milestone 5 released!

We are extremely pleased to announce the fifth 4.0 milestone release of Eclipse Vert.x. This release aims to provide a reliable distribution of the current development of Vert.x 4.

Read more