【问题标题】:How to get instance of javax.ws.rs.core.UriInfo如何获取 javax.ws.rs.core.UriInfo 的实例
【发布时间】:2023-03-03 05:46:21
【问题描述】:

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

更新:我想为类似这样的函数编写单元测试:

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

【问题讨论】:

    标签: java unit-testing jax-rs jersey-2.0 grizzly


    【解决方案1】:

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

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

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

    编辑

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

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

    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>
      

    如果你想做一个集成测试,注入实际的 UriInfo,你应该查看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"));
        }
    }
    

    只需添加此依赖项

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

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

    【讨论】:

    • 其实我想为一个类似于你的 doSomthing() 函数的函数写一个junit测试。
    • 啊,我错过了那部分。您可以使用像 Mockito 这样的 Mocking 框架,或者只创建一个存根,仅实现您正在使用的方法。对于单元测试,这是我唯一能想到的。对于集成测试,您可以使用 Jersey 测试框架,它将启动应用程序并为测试提供完整功能(包括注入)
    【解决方案2】:

    你要么模拟它,要么使用http://arquillian.org/之类的东西

    【讨论】:

      【解决方案3】:

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

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

      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 的任何错误,请参阅 request scoped beans in spring testing

      【讨论】:

        猜你喜欢
        • 2019-07-08
        • 1970-01-01
        • 2012-10-11
        • 2015-01-19
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多