如何获取javax.ws.rs.core.UriInfo的实例 [英] How to get instance of javax.ws.rs.core.UriInfo

查看:296
本文介绍了如何获取javax.ws.rs.core.UriInfo的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在javax.ws.rs.core.UriInfo的任何实现,可用于快速创建实例进行测试.这个接口很长,我只需要测试一下即可.我不想在此接口的整个实现上浪费时间.

Is there any implementation of javax.ws.rs.core.UriInfo which I can use to create an instance quickly for testing. This interface is long, I just need to test something. I don't want to waste time on whole implementation of this interface.

更新:我想为此功能编写单元测试:

UPDATE: I want to write a unit test for a function similar to this:

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

推荐答案

您只需将@Context注释作为字段或方法参数注入即可.

You simply inject it with the @Context annotation, as a field or method parameter.

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

    public Response doSomthing(@Context UriInfo uriInfo) {

    }
}

除了您的资源类之外,还可以将其注入到其他提供程序中,例如ContainerRequestContextContextResolverMessageBodyReader等.

Other than your resource classes, it can also be injected into other providers, like ContainerRequestContext, ContextResolver, MessageBodyReader etc.

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

Actually I want to write a junit test for a function similar to your doSomthing() function.

我没有在您的帖子中讲到这一点.但是对于单元测试,我可以想到几个选择

I didn't pick that up in your post. But a couple options I can think of for unit tests

  1. 仅创建一个存根,仅实现您使用的方法.

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

使用类似 Mockito 的Mocking框架,并模拟UriInfo.例子

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);
}

您需要添加此依赖项

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.0</version>
</dependency>

如果要进行集成测试,并在其中注入实际的UriInfo,则应查看 Jersey测试框架

If you want to do an integration test, where the actual UriInfo is injected, you should look into Jersey Test Framework

这是Jersey测试框架的完整示例

Here's a complete example with the 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支持的容器.只需查看我上面发布的链接即可.

It uses an in-memory container, which is the most efficient for small tests. There are other containers with Servlet support if needed. Just see the link I posted above.

这篇关于如何获取javax.ws.rs.core.UriInfo的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆