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:

Copy to Clipboard Toggle word wrap
<dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.4.0</version>
      <scope>test</scope>
    </dependency>
  • 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.

Prerequisites

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

Procedure

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

    Copy to Clipboard Toggle word wrap
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
    </plugin>
  2. Create a directory structure src/test/java/com/example/ in the root directory, and navigate to it.

    Copy to Clipboard Toggle word wrap
    $ mkdir -p src/test/java/com/example/
    $ cd src/test/java/com/example/
  3. Create a Java class file MyTestApp.java containing the application code.

    Copy to Clipboard Toggle word wrap
    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;
    
    @ExtendWith(VertxExtension.class)
    class MyAppTest {
    
      @BeforeEach
      void prepare(Vertx vertx, VertxTestContext testContext) {
        // Deploy the verticle
        vertx.deployVerticle(new MyApp())
          .onSuccess(ok -> testContext.completeNow())
          .onFailure(failure -> testContext.failNow(failure));
      }
    
      @Test
      @DisplayName("Smoke test: check that the HTTP server responds")
      void smokeTest(Vertx vertx, VertxTestContext testContext) {
        // Issue an HTTP request
        vertx.createHttpClient()
          .request(HttpMethod.GET, 8080, "127.0.0.1", "/")
          .compose(request -> request.send())
          .compose(response -> response.body())
          .onSuccess(body -> testContext.verify(() -> {
            // Check the response
            assertEquals("Greetings!", body.toString());
            testContext.completeNow();
          }))
          .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.

    Copy to Clipboard Toggle word wrap
    mvn clean verify

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

Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat, Inc.