Тест Junit WebClient не отправляет запрос или не получает ответ. Получение «ошибки подключаемого модуля» при выполнении «maven test» - PullRequest
0 голосов
/ 06 января 2019

Итак, я запускаю простой тест джунта. Тест содержит WebClient, который должен отправить простой запрос моему API. Я проверил API с помощью Postman, и он работает, он отвечает. Однако по какой-то причине я не могу заставить моего WebClient отправить запрос. Когда я запускаю тест по отдельности, тест не показывает сбой (в консоли не отображаются ошибки), но запрос не отправляется моему API. Согласно моему Logger, .post не выполняется. Чего мне не хватает?

Вот код для тестирования моего WebClient:

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.junit5.VertxExtension;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

@ExtendWith(VertxExtension.class)
public class LoginCoreClientTest  {

    private LoginCoreClient client;
    private static io.vertx.core.logging.Logger logger = LoggerFactory.getLogger(LoginCoreClient.class);

    @Test
    @DisplayName("Testing API response from client")
    public void test() {
        JsonObject request = new JsonObject().put("name", "Bob Smith").put("Address", "101 Broadway"));

        Vertx vertx = Vertx.vertx();
        WebClient client = WebClient.create(vertx, new WebClientOptions()
                  .setDefaultHost("localhost")
                  .setDefaultPort(8080));

        logger.info("Here!"); //this line gets printed
        client.post("/endpoint")
        .putHeader("content-type", "application/json")
        .sendJson(request, ar -> {
            if (ar.succeeded()) {
                logger.info("Now here"); //this line does NOT get printed
                logger.info("succeeded: " + ar.succeeded());
                logger.info("statusCode: " + ar.result().statusCode());
                logger.info("body: " + ar.result().body());
                logger.info("headers: " + ar.result().headers());
             } else
            {
                logger.info("Executed: " + ar.cause());
            }
          });   
    }
}

Также, когда я запускаю «maven test», я получаю ошибку плагина, такую ​​как эта Error that appears in Eclipse console

А вот и pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.MyProject</groupId>
    <artifactId>consumerprofile</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>consumerprofile</name>
    <description>Service to retrieve consumer profile</description>

    <properties>
        <vertx.version>3.6.0</vertx.version>
        <vertx.verticle>com.company.MyProject.ConsumerProfileVerticle</vertx.verticle>
        <vertx-maven-plugin.version>1.0.13</vertx-maven-plugin.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <junit-jupiter.version>5.1.0</junit-jupiter.version>
        <assertj-core.version>3.8.0</assertj-core.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--maven.surefire.version>3.0.0-M2</maven.surefire.version -->
        <!-- Db properties -->
        <tag.version></tag.version>
        <rollbackto.version></rollbackto.version>
        <rollbackto.date></rollbackto.date>
        <changelog.file>database/changelog.xml</changelog.file>
        <sqloutput>tmp/tagout.txt</sqloutput>
        <db.url>jdbc:postgresql://localhost:5432/cudadb</db.url>
        <db.driver>org.postgresql.Driver</db.driver>
        <db.user></db.user>
        <db.password></db.password>
        <db.sql></db.sql>
        <db.onerror>continue</db.onerror>
        <postgresql.version>42.2.5</postgresql.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.vertx</groupId>
                <artifactId>vertx-dependencies</artifactId>
                <version>${vertx.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web-client</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-config</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${assertj-core.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-jdbc-client</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-sql-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>vertx-maven-plugin</artifactId>
                <version>${vertx-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>initialize</goal>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <redeploy>true</redeploy>
                </configuration>
            </plugin>
            <!-- Liquibase plugin -->
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.6.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <changeLogFile>${changelog.file}</changeLogFile>
                    <driver>${db.driver}</driver>
                    <url>${db.url}</url>
                    <username>${db.user}</username>
                    <password>${db.password}</password>
                    <tag>${tag.version}</tag>
                    <rollbackTag>${rollbackto.version}</rollbackTag>
                    <rollbackDate>${rollbackto.date}</rollbackDate>
                    <goalPrefix>liquibase</goalPrefix>
                </configuration>
                <executions>
                    <execution>
                        <id>apply-db</id>
                        <phase>update-sql</phase>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>apply-tag</id>
                        <phase>tag-sql</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>rollback-db</id>
                        <phase>rollback-sql</phase>
                        <goals>
                            <goal>rollback</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- posgtgresql plug in -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.5</version>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>42.2.5</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <driver>${db.driver}</driver>
                    <url>${db.url}</url>
                    <username>${db.user}</username>
                    <password>${db.password}</password>
                    <sqlCommand>${db.sql}</sqlCommand>
                    <goalPrefix>sql</goalPrefix>
                    <printResultSet>true</printResultSet>
                    <outputFile>${sqloutput}</outputFile>
                    <onError>${db.onerror}</onError>
                </configuration>
                <executions>
                    <execution>
                        <id>fetch-db</id>
                        <phase>fetch-tag</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> 
                <version>${maven.surefire.version}</version> </plugin >
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.3</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>io.fabric8</groupId>
                                        <artifactId>
                                            vertx-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.0.13,)
                                        </versionRange>
                                        <goals>
                                            <goal>initialize</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>openshift</id>
            <build>
                <plugins>
                    <!-- Fabric8 Maven Plugin -->
                    <!-- Deploy to openshift or kubernetes: "mvn clean fabric8:deploy" -->
                    <!-- Note: See src/main/fabric8/deployment.yml for deployment config -->
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>fabric8-maven-plugin</artifactId>
                        <version>3.5.41</version>
                        <executions>
                            <execution>
                                <id>fabric8</id>
                                <goals>
                                    <goal>resource</goal>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <!-- Deploy to Nexus -->
    <distributionManagement>
        <repository>
            <id>MyProjectreleases</id>
            <url>http://artifactory.company.local/artifactory/MyProjectReleases/</url>
        </repository>
        <snapshotRepository>
            <id>MyProjectsnapshots</id>
            <url>http://artifactory.company.local/artifactory/MyProjectSnapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>
...