使用 Jersey 测试框架进行测试的 Maven 依赖项

Maven dependency for testing using Jersey Test Framework

我正在尝试使用 Jersey 测试框架测试 Restful 服务,我有以下代码。

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import javax.json.JsonException;
import org.junit.Test;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.*;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;


public class MakeSureTest extends JerseyTest{

    @Override
    protected AppDescriptor configure() {
        return new WebAppDescriptor.Builder().build();
    }

    @Test
    public void testUserFetchesSuccess() throws JsonException,
            URISyntaxException {


        WebResource webResource = client().resource("http://localhost:8080");
        String encodedParam = null;
        try {
            encodedParam = URLEncoder.encode("customertransactions/retrievetransactions/2015/04/25", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        VigLinkComission vigLinkComission = webResource.path(encodedParam)
                .get(MyComission.class);
    }

/*  @Test(expected = UniformInterfaceException.class)
    public void testUserNotFound() {
        WebResource webResource = client().resource("http://localhost:8080/");
        JsonObject json = webResource.path("/rest-test-tutorial/user/id/666")
                .get(JsonObject.class);
    }
*/
}

我有以下 Maven 依赖项。

<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.mycompany</groupId>
    <artifactId>MyTracker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MyTracker</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>

    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>com.yammer.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>0.6.2</version>
        </dependency>
        <dependency>
            <groupId>net.vz.mongodb.jackson</groupId>
            <artifactId>mongo-jackson-mapper</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.jongo</groupId>
            <artifactId>jongo</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.joda</groupId>
            <artifactId>joda-money</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
            <artifactId>jersey-test-framework-core</artifactId>
            <version>1.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
            <artifactId>jersey-test-framework-external</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
            <artifactId>jersey-test-framework-http</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
            <artifactId>jersey-test-framework-grizzly2</artifactId>
            <version>1.19</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.17.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-grizzly2</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.17.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.test.framework</groupId>
            <artifactId>jersey-test-framework</artifactId>
            <version>1.0.3.1</version>

        </dependency>       
    </dependencies>

</project>

然而,当我在 eclipse 中执行 Maven 全新安装时,我看到以下编译错误,顺便说一下,eclipse IDE 中没有显示,有人可以告诉我为什么我在Maven 构建? Maven 依赖列表中还缺少什么让我使用 Jersey 测试框架?

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project MyTracker: Compilation failure: Compilation failure:
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[12,36] error: cannot find symbol
[ERROR] package com.sun.jersey.test.framework
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[14,36] error: cannot find symbol
[ERROR] package com.sun.jersey.test.framework
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[20,11] error: cannot find symbol
[ERROR] class MyTest
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[21,29] error: package WebAppDescriptor does not exist
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[19,1] error: method does not override or implement a method from a supertype
[ERROR] /home/general/workspace/MyTracker/src/main/java/com/mycompany/MyTracker/Test/MyTest.java:[29,28] error: cannot find symbol

我怀疑您的 Maven 项目没有正确的目录结构。

有问题的 class (MakeSureTest) 应该在 src/test/java 目录下,也在适当的包结构下。

尝试以这种方式组织您的 classes 并发布 mvn clean compile