0

我正在构建一个使用 Maven 构建并使用 Jersey 进行 RESTful API 的 Web 项目。我已经使用 TestNG 和 Jersey 测试框架编写了单元测试,并且在内存中而不是在 Web 服务器上运行它们。

在我的测试中,我对我的 MySQL 数据库进行了 JDBC 调用并得到了一些结果。当我使用 TestNG for Eclipse 插件在 Eclipse 中运行时,测试运行良好。但是,当我尝试使用以下两个命令之一从 Maven 运行时:

    mvn clean package

或者

    mvn test

我得到如下所示的输出:

在此处输入图像描述

我的 pom.xml 如下:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>co.teamlink.services</groupId>
    <artifactId>teamlink-services</artifactId>
    <packaging>war</packaging>
    <version>0.5.0</version>
    <name>teamlink-services</name>
    <build>
        <finalName>teamlink-services-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.test-framework.providers</groupId>
                <artifactId>jersey-test-framework-provider-inmemory</artifactId>
                <version>${jersey.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.44</version>
                <scope>runtime, test</scope>
            </dependency>
            <dependency>
                <groupId>org.simplejavamail</groupId>
                <artifactId>simple-java-mail</artifactId>
                <version>4.4.5</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>0.9.0</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-inmemory</artifactId>
        </dependency>
        <dependency>
            <groupId>org.simplejavamail</groupId>
            <artifactId>simple-java-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.25.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

感谢您的帮助,因为这让我发疯。我不确定我到底错过了什么。

4

1 回答 1

2

pom.xml您的文件有几个问题。

首先mysql-connector-javain dependencyManagementsection 的范围定义不正确:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
            <scope>runtime, test</scope>
        </dependency>

Maven 不允许定义逗号分隔的范围列表。在您的情况下,指定范围就足够了,runtime因为库将在测试执行和运行时可用。所以应该是

            <scope>runtime</scope>

下一节dependencyManagement仅用于声明项目及其子项目中依赖项的“标准”用法。要将库实际包含在您的项目中,它应该列在 project 中dependencies。而你的 pom 缺乏

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

在项目依赖项之间。

于 2017-11-12T08:07:55.080 回答