Menu Close
Settings Close

Language and Page Formatting Options

Chapter 4. Testing your Eclipse Vert.x application with JUnit

After you build your Eclipse Vert.x application in the getting-started project, test your application with the JUnit 5 framework to ensure that it runs as expected. The following two dependencies in the Eclipse Vert.x pom.xml file are used for JUnit 5 testing:

  • The vertx-junit5 dependency is required for testing. JUnit 5 provides various annotations, such as, @Test, @BeforeEach, @DisplayName, and so on which are used to request asynchronous injection of Vertx and VertxTestContext instances.
  • The junit-jupiter-engine dependency is required for execution of tests at runtime.


  • You have built the Eclipse Vert.x getting-started project using the pom.xml file.


  1. Open the generated pom.xml file and set the version of the Surefire Maven plug-in:

  2. Create a directory structure src/test/java/com/example/ in the root directory, and navigate to it.

    $ mkdir -p src/test/java/com/example/
    $ cd src/test/java/com/example/
  3. Create a Java class file containing the application code.

    package com.example;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import io.vertx.core.Vertx;
    import io.vertx.core.http.HttpMethod;
    import io.vertx.junit5.VertxExtension;
    import io.vertx.junit5.VertxTestContext;
    class MyAppTest {
      void prepare(Vertx vertx, VertxTestContext testContext) {
        // Deploy the verticle
        vertx.deployVerticle(new MyApp())
          .onSuccess(ok -> testContext.completeNow())
          .onFailure(failure -> testContext.failNow(failure));
      @DisplayName("Smoke test: check that the HTTP server responds")
      void smokeTest(Vertx vertx, VertxTestContext testContext) {
        // Issue an HTTP request
          .request(HttpMethod.GET, 8080, "", "/")
          .compose(request -> request.send())
          .compose(response -> response.body())
          .onSuccess(body -> testContext.verify(() -> {
            // Check the response
            assertEquals("Greetings!", body.toString());
          .onFailure(failure -> testContext.failNow(failure));
  4. To run the JUnit test on my application using Maven run the following command from the root directory of the application.

    mvn clean verify

    You can check the test results in the target/surefire-reports. The com.example.MyAppTest.txt file contains the test results.