搜索

此内容没有您所选择的语言版本。

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

download PDF

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:

<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:

    <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.

    $ 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.

    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.

    mvn clean verify

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

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.