如何获取javax.ws.rs.core.UriInfo的实例

13

有没有javax.ws.rs.core.UriInfo的任何实现可以让我快速创建一个用于测试的实例。这个接口很长,我只需要测试一些东西。我不想在这个接口的整个实现上浪费时间。

更新:我想为一个类似于这个函数的单元测试编写代码:

@GET
@Path("/my_path")
@Produces(MediaType.TEXT_XML)
public String webserviceRequest(@Context UriInfo uriInfo);
3个回答

14

您只需使用@Context注释将其作为字段或方法参数进行注入。

@Path("resource")
public class Resource {
    @Context
    UriInfo uriInfo;

    public Response doSomthing(@Context UriInfo uriInfo) {

    }
}

除了资源类,它还可以注入到其他提供程序中,例如ContainerRequestContextContextResolverMessageBodyReader等。

编辑

实际上,我想为一个类似于你的doSomthing()函数的函数编写一个JUnit测试。

我在你的帖子中没有注意到这一点。但我能想到几个选项用于单元测试。

  1. Simply create a stub, implementing only the methods you use.

  2. Use a Mocking framework like Mockito, and mock the UriInfo. Example

    @Path("test")
    public class TestResource { 
        public String doSomthing(@Context UriInfo uriInfo){
            return uriInfo.getAbsolutePath().toString();
        }
    }
    [...]
    @Test
    public void doTest() {
        UriInfo uriInfo = Mockito.mock(UriInfo.class);
        Mockito.when(uriInfo.getAbsolutePath())
            .thenReturn(URI.create("http://localhost:8080/test"));
        TestResource resource = new TestResource();
        String response = resource.doSomthing(uriInfo);
        Assert.assertEquals("http://localhost:8080/test", response);
    }
    

    You'll need to add this dependency

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.0</version>
    </dependency>
    
如果您想进行集成测试,并注入实际的UriInfo,您应该查看Jersey Test Framework
以下是使用Jersey Test Framework的完整示例。
public class ResourceTest extends JerseyTest {

    @Path("test")
    public static class TestResource {
        @GET
        public Response doSomthing(@Context UriInfo uriInfo) {
            return Response.ok(uriInfo.getAbsolutePath().toString()).build();
        }
    }

    @Override
    public Application configure() {
        return new ResourceConfig(TestResource.class);
    }

    @Test
    public void test() {
        String response = target("test").request().get(String.class);
        Assert.assertTrue(response.contains("test"));
    }
}

只需添加此依赖项

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-inmemory</artifactId>
    <version>${jersey2.version}</version>
</dependency>

它使用内存容器,对于小测试来说最有效。如果需要支持Servlet,则有其他容器可用。只需查看我上面发布的链接即可。

实际上,我想为类似于你的doSomething()函数的一个函数编写一个JUnit测试。 - Aqeel Ashiq
1
啊,我错过了那一部分。你可以使用像Mockito这样的模拟框架,或者只是创建一个存根(stub),仅实现你正在使用的方法。那是我能想到的唯一适用于单元测试的做法。对于集成测试,你可以使用Jersey测试框架,它将启动应用程序并为测试(包括注入)提供完全功能。 - Paul Samsotha

0

我正在编写集成测试,因此无法使用模拟工具

我使用了一些用于Jersey测试的代码

http://www.programcreek.com/java-api-examples/index.php?source_dir=JerseyTest-master/jersey-tests/src/test/java/com/sun/jersey/impl/uri/UriPathHttpRequestTest.java

WebApplicationImpl wai = new WebApplicationImpl();
ContainerRequest r = new TestHttpRequestContext(wai,
            "GET", null,
            "/mycontextpath/rest/data", "/mycontextpath/");
UriInfo uriInfo = new WebApplicationContext(wai, r, null);
myresources.setUriInfo(uriInfo);

并且

private static class TestHttpRequestContext extends ContainerRequest {
    public TestHttpRequestContext(
            WebApplication wa,
            String method,
            InputStream entity,
            String completeUri,
            String baseUri) {
        super(wa, method, URI.create(baseUri), URI.create(completeUri), new InBoundHeaders(), entity);
    }
}

如果您遇到有关请求范围 Bean 的任何错误,请参见Spring 测试中的请求范围 Bean


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接