使用Jersey Test Framework,TestNG和Maven进行内存测试

时间:2017-11-12 06:02:53

标签: java maven jersey testng jersey-test-framework

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

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

    mvn clean package

    mvn test

我得到如下所示的输出:

enter image description here

我的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>

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

1 个答案:

答案 0 :(得分:2)

您的using ::testing::DefaultValue; struct Foo { MOCK_METHOD0(doWork, int()); }; DefaultValue<int>::Set(10); // error: specializing member 'testing::DefaultValue<int>::Set' requires 'template<>' syntax; TEST(BarTest, DoesThis) { Foo foo; DefaultValue<int>::Set(10); // everything works fine. EXPECT_CALL(foo, doWork()); foo.doWork(); } 文件存在多个问题。

首先,pom.xml部分中mysql-connector-java的范围定义不正确:

dependencyManagement

Maven不允许定义以逗号分隔的范围列表。在您的情况下,指定 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> <scope>runtime, test</scope> </dependency> 范围就足够了,因为在测试执行和运行时期间库将可用。所以它应该是

runtime

下一个 <scope>runtime</scope> 部分仅用于声明&#34;标准&#34;在项目及其子项目中使用依赖项。要在您的项目中实际包含库,它应该列在项目dependencyManagement中。你的pom缺乏

dependencies
项目依赖项中的