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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29844097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:00:36  来源:igfitidea点击:

How to get instance of javax.ws.rs.core.UriInfo

javaunit-testingjax-rsjersey-2.0grizzly

提问by Syed Aqeel Ashiq

Is there any implementation of javax.ws.rs.core.UriInfowhich 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.

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

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

回答by Paul Samsotha

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

您只需将@Context注释注入它,作为字段或方法参数。

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

    public Response doSomthing(@Context UriInfo uriInfo) {

    }
}

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

除了你的资源类,它也可以被注入到其它供应商一样ContainerRequestContextContextResolverMessageBodyReader等。

EDIT

编辑

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

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

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

我没有在你的帖子中提到这一点。但是我可以为单元测试想到几个选项

  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>
    
  1. 只需创建一个存根,仅实现您使用的方法。

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

    @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>
    

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

如果你想做一个集成测试,在那里注入实际的 UriInfo,你应该查看Jersey 测试框架

Here's a complete example with the Jersey Test Framework

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

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

Just add this dependency

只需添加此依赖项

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

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.

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

回答by Friso

You either mock it, or use something like http://arquillian.org/

你要么嘲笑它,要么使用类似http://arquillian.org/ 的东西

回答by Kalpesh Soni

I was writing integration tests, so cannot use mock stuff

我正在编写集成测试,所以不能使用模拟的东西

I used some code for jersey tests

我使用了一些代码进行球衣测试

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

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

and

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

If you get any errors about request scope beans see request scoped beans in spring testing

如果您收到有关请求范围 bean 的任何错误,请参阅Spring 测试中的请求范围 bean